Last active
December 6, 2017 12:48
-
-
Save MensObscura/02527812635d0fedaa73677b07352735 to your computer and use it in GitHub Desktop.
TabLayout WithPadding and no white bar on ripple
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context; | |
import android.support.annotation.NonNull; | |
import android.support.annotation.Nullable; | |
import android.support.design.widget.TabLayout; | |
import android.support.v4.view.PagerAdapter; | |
import android.support.v4.view.ViewPager; | |
import android.util.AttributeSet; | |
import android.view.Gravity; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.view.ViewTreeObserver; | |
import android.widget.LinearLayout; | |
import com.you.package.UIUTils; | |
import java.lang.reflect.Field; | |
public class PaddingTabLayout extends TabLayout { | |
private LinearLayout slidingTabStrip; | |
private OnLayoutChangeListener mOnLayoutChangeListener; | |
private ViewTreeObserver.OnPreDrawListener mOnPreDrawListener; | |
private ViewTreeObserver.OnScrollChangedListener mOnScrollChangedListener; | |
public CustomTabLayout(Context context) { | |
super(context); | |
} | |
public CustomTabLayout(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
setStripTabMaxSize(); | |
} | |
public CustomTabLayout(Context context, AttributeSet attrs, int defStyleAttr) { | |
super(context, attrs, defStyleAttr); | |
} | |
private void setStripTabMaxSize() { | |
final int padding = UIUTils.convertDpToPx(getContext(), 20); | |
final int maxSize = UIUTils.getDeviceWidth(getContext()); | |
Field field; | |
try { | |
field = TabLayout.class.getDeclaredField("mTabStrip"); | |
field.setAccessible(true); | |
Class tClazz = null; | |
for (Class<?> aClass : TabLayout.class.getDeclaredClasses()) { | |
if (aClass.getName().contains("SlidingTabStrip")) { | |
tClazz = aClass; | |
break; | |
} | |
} | |
final Class clazz = tClazz; | |
slidingTabStrip = (LinearLayout) field.get(this); | |
mOnLayoutChangeListener = new OnLayoutChangeListener() { | |
@Override | |
public void onLayoutChange(View view, int i, int i1, int i2, int i3, int i4, int i5, int i6, int i7) { | |
slidingTabStrip.removeOnLayoutChangeListener(this); | |
resizeBar(clazz, slidingTabStrip, maxSize, padding); | |
} | |
}; | |
slidingTabStrip.addOnLayoutChangeListener(mOnLayoutChangeListener); | |
mOnPreDrawListener = new ViewTreeObserver.OnPreDrawListener() { | |
@Override | |
public boolean onPreDraw() { | |
resizeBar(clazz, slidingTabStrip, maxSize, padding); | |
return true; | |
} | |
}; | |
slidingTabStrip.getViewTreeObserver().addOnPreDrawListener(mOnPreDrawListener); | |
mOnScrollChangedListener = new ViewTreeObserver.OnScrollChangedListener() { | |
@Override | |
public void onScrollChanged() { | |
resizeBar(clazz, slidingTabStrip, maxSize, padding); | |
} | |
}; | |
slidingTabStrip.getViewTreeObserver().addOnScrollChangedListener(mOnScrollChangedListener); | |
} catch (NoSuchFieldException e) { | |
e.printStackTrace(); | |
} catch (IllegalAccessException e) { | |
e.printStackTrace(); | |
} | |
} | |
private void resizeBar(Class clazz, LinearLayout slidingTabStrip, int maxSize, int padding) { | |
int maxRight = maxSize - padding; | |
if (clazz != null) { | |
try { | |
Field mIndicatorLeft = clazz.getDeclaredField("mIndicatorLeft"); | |
mIndicatorLeft.setAccessible(true); | |
int left = (int) mIndicatorLeft.get(slidingTabStrip); | |
Field mIndicatorRight = clazz.getDeclaredField("mIndicatorRight"); | |
mIndicatorRight.setAccessible(true); | |
int right = (int) mIndicatorRight.get(slidingTabStrip); | |
if (left < padding) { | |
slidingTabStrip.setGravity(Gravity.LEFT); | |
mIndicatorLeft.set(slidingTabStrip, padding); | |
} | |
if (right > maxRight) { | |
slidingTabStrip.setGravity(Gravity.RIGHT); | |
mIndicatorRight.set(slidingTabStrip, maxRight); | |
} | |
} catch (NoSuchFieldException e) { | |
e.printStackTrace(); | |
} catch (IllegalAccessException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
@Override | |
public void setupWithViewPager(@Nullable ViewPager viewPager) { | |
super.setupWithViewPager(viewPager); | |
if (viewPager != null && getPageAdapterChangeListener() != null && !UIUTils.isTabletDevice(getContext())) { | |
viewPager.addOnAdapterChangeListener(getPageAdapterChangeListener()); | |
} | |
} | |
private OnPageAdapterChangeListener mOnPageAdapterChangeListener = new OnPageAdapterChangeListener(); | |
private OnPageAdapterChangeListener getPageAdapterChangeListener() { | |
return mOnPageAdapterChangeListener; | |
} | |
private class OnPageAdapterChangeListener implements ViewPager.OnAdapterChangeListener { | |
@Override | |
public void onAdapterChanged(@NonNull ViewPager viewPager, | |
@Nullable PagerAdapter oldAdapter, @Nullable PagerAdapter newAdapter) { | |
final int padding = UIUTils.convertDpToPx(getContext(), 20); | |
View rootView = getChildAt(0); | |
if (rootView != null && newAdapter != null) { | |
View startTab = ((ViewGroup) rootView).getChildAt(0); | |
if (startTab != null) { | |
startTab.setPadding(padding, getPaddingTop(), getPaddingRight(), getPaddingBottom()); | |
} | |
View endTab = ((ViewGroup) rootView).getChildAt(newAdapter.getCount() - 1); | |
if (endTab != null) { | |
endTab.setPadding(getPaddingLeft(), getPaddingTop(), padding, getPaddingBottom()); | |
} | |
} | |
} | |
} | |
@Override | |
protected void onDetachedFromWindow() { | |
super.onDetachedFromWindow(); | |
if (slidingTabStrip != null) { | |
slidingTabStrip.removeOnLayoutChangeListener(mOnLayoutChangeListener); | |
mOnLayoutChangeListener = null; | |
slidingTabStrip.getViewTreeObserver().removeOnPreDrawListener(mOnPreDrawListener); | |
mOnPreDrawListener = null; | |
slidingTabStrip.getViewTreeObserver().removeOnScrollChangedListener(mOnScrollChangedListener); | |
mOnScrollChangedListener = null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment