diff --git a/library/build.gradle b/library/build.gradle index bc996cd..d5273d0 100755 --- a/library/build.gradle +++ b/library/build.gradle @@ -19,6 +19,7 @@ android { proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } + namespace 'com.ogaclejapan.smarttablayout' } dependencies { @@ -27,13 +28,3 @@ dependencies { implementation deps.androidx.fragment.lib } -license { - - sourceSets { - main.java.srcDirs = android.sourceSets.main.java.srcDirs - main.resources.srcDirs = android.sourceSets.main.resources.srcDirs - } - - excludes(["**/*.xml"]) - -} diff --git a/library/src/main/java/com/ogaclejapan/smarttablayout/SmartTabLayout2.java b/library/src/main/java/com/ogaclejapan/smarttablayout/SmartTabLayout2.java index 8af07d6..349e63c 100755 --- a/library/src/main/java/com/ogaclejapan/smarttablayout/SmartTabLayout2.java +++ b/library/src/main/java/com/ogaclejapan/smarttablayout/SmartTabLayout2.java @@ -89,10 +89,14 @@ public class SmartTabLayout2 extends HorizontalScrollView { private OnScrollChangeListener onScrollChangeListener; private TabProvider tabProvider; private InternalTabClickListener internalTabClickListener; + private InternalTabLongClickListener internalTabLongClickListener; private OnTabClickListener onTabClickListener; + private OnTabLongClickListener onTabLongClickListener; private boolean distributeEvenly; private int textAppearance; + private int childCountCache = 0; + public SmartTabLayout2(Context context) { this(context, null); } @@ -161,6 +165,7 @@ public SmartTabLayout2(Context context, AttributeSet attrs, int defStyle) { this.tabViewTextHorizontalPadding = textHorizontalPadding; this.tabViewTextMinWidth = textMinWidth; this.internalTabClickListener = clickable ? new InternalTabClickListener() : null; + this.internalTabLongClickListener = clickable ? new InternalTabLongClickListener() : null; this.distributeEvenly = distributeEvenly; this.textAppearance = textStyle; @@ -193,15 +198,19 @@ protected void onScrollChanged(int l, int t, int oldl, int oldt) { @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); - if (tabStrip.isIndicatorAlwaysInCenter() && tabStrip.getChildCount() > 0) { - View firstTab = tabStrip.getChildAt(0); - View lastTab = tabStrip.getChildAt(tabStrip.getChildCount() - 1); - int start = (w - Utils.getMeasuredWidth(firstTab)) / 2 - Utils.getMarginStart(firstTab); - int end = (w - Utils.getMeasuredWidth(lastTab)) / 2 - Utils.getMarginEnd(lastTab); - tabStrip.setMinimumWidth(tabStrip.getMeasuredWidth()); - ViewCompat.setPaddingRelative(this, start, getPaddingTop(), end, getPaddingBottom()); - setClipToPadding(false); - } + adjustForIndicatorAlwaysInCenter(w, h); + } + + private void adjustForIndicatorAlwaysInCenter(int w, int h) { + if (tabStrip.isIndicatorAlwaysInCenter() && tabStrip.getChildCount() > 0) { + View firstTab = tabStrip.getChildAt(0); + View lastTab = tabStrip.getChildAt(tabStrip.getChildCount() - 1); + int start = (w - Utils.getMeasuredWidth(firstTab)) / 2 - Utils.getMarginStart(firstTab); + int end = (w - Utils.getMeasuredWidth(lastTab)) / 2 - Utils.getMarginEnd(lastTab); + tabStrip.setMinimumWidth(tabStrip.getMeasuredWidth()); + ViewCompat.setPaddingRelative(this, start, getPaddingTop(), end, getPaddingBottom()); + setClipToPadding(false); + } } @Override @@ -211,6 +220,10 @@ protected void onLayout(boolean changed, int l, int t, int r, int b) { if (changed && viewPager != null) { scrollToTab(viewPager.getCurrentItem(), 0); } + if (childCountCache != tabStrip.getChildCount()) { + adjustForIndicatorAlwaysInCenter(getMeasuredWidth(), getMeasuredHeight()); + } + childCountCache = tabStrip.getChildCount(); } /** @@ -305,6 +318,10 @@ public void setOnTabClickListener(OnTabClickListener listener) { onTabClickListener = listener; } + public void setOnTabLongClickListener(OnTabLongClickListener listener) { + onTabLongClickListener = listener; + } + /** * Set the custom layout to be inflated for the tab views. * @@ -333,7 +350,9 @@ public void setViewPager(@NonNull ViewPager2 viewPager, @NonNull RecyclerView.Ad tabStrip.removeAllViews(); this.viewPager = viewPager; - this.viewPager.setAdapter(adapter); + if (this.viewPager.getAdapter() != adapter){ + this.viewPager.setAdapter(adapter); + } this.tabTitleProvider = titleProvider; if (viewPager != null && viewPager.getAdapter() != null) { viewPager.registerOnPageChangeCallback(new InternalViewPagerListener()); @@ -422,6 +441,9 @@ private void populateTabStrip() { if (internalTabClickListener != null) { tabView.setOnClickListener(internalTabClickListener); } + if (internalTabLongClickListener != null) { + tabView.setOnLongClickListener(internalTabLongClickListener); + } tabStrip.addView(tabView); @@ -538,6 +560,19 @@ public interface OnTabClickListener { void onTabClicked(int position); } + /** + * Interface definition for a callback to be invoked when a tab is long clicked. + */ + public interface OnTabLongClickListener { + + /** + * Called when a tab is clicked. + * + * @param position tab's position + */ + void onTabLongClicked(int position); + } + /** * Create the custom tabs in the tab layout. Set with * {@link #setCustomTabView(SmartTabLayout2.TabProvider)} @@ -650,4 +685,19 @@ public void onClick(View v) { } } } + + private class InternalTabLongClickListener implements OnLongClickListener { + @Override + public boolean onLongClick(View v) { + for (int i = 0; i < tabStrip.getChildCount(); i++) { + if (v == tabStrip.getChildAt(i)) { + if (onTabLongClickListener != null) { + onTabLongClickListener.onTabLongClicked(i); + } + return true; + } + } + return false; + } + } } diff --git a/utils-v4/build.gradle b/utils-v4/build.gradle index 4acc252..bc28e4e 100755 --- a/utils-v4/build.gradle +++ b/utils-v4/build.gradle @@ -18,6 +18,7 @@ android { proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } + namespace 'com.ogaclejapan.smarttablayout.utils' } dependencies {