Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 1 addition & 10 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ android {
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
namespace 'com.ogaclejapan.smarttablayout'
}

dependencies {
Expand All @@ -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"])

}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand All @@ -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();
}

/**
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -422,6 +441,9 @@ private void populateTabStrip() {
if (internalTabClickListener != null) {
tabView.setOnClickListener(internalTabClickListener);
}
if (internalTabLongClickListener != null) {
tabView.setOnLongClickListener(internalTabLongClickListener);
}

tabStrip.addView(tabView);

Expand Down Expand Up @@ -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)}
Expand Down Expand Up @@ -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;
}
}
}
1 change: 1 addition & 0 deletions utils-v4/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ android {
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
namespace 'com.ogaclejapan.smarttablayout.utils'
}

dependencies {
Expand Down