依赖:
implementation 'com.android.support:design:27.1.1'
xml布局
<android.support.design.widget.TabLayout
android:id="@+id/order_tab"
android:layout_width="match_parent"
android:layout_height="@dimen/dp_28"
android:layout_below="@id/text1"
app:tabIndicatorColor="@color/colorRed"
app:tabMode="fixed"
app:tabSelectedTextColor="@color/colorRed"
app:tabTextColor="@color/colorBlack">
</android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:id="@+id/order_vpger"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/text">
</android.support.v4.view.ViewPager>
添加数据:
private void initData() {
tab_sell = new ArrayList<>();
tab_sell.add("全部");
tab_sell.add("代付款");
tab_sell.add("代发货");
tab_sell.add("代收货");
tab_sell.add("代评价");
fragment_sell = new ArrayList<>();
fragment_sell.add(new OrderAllFragment());
fragment_sell.add(new OrderDfkFragment());
fragment_sell.add(new OrderDfhFragment());
fragment_sell.add(new OrderDshFragment());
fragment_sell.add(new OrderDpjFragment());
}
初始化,绑定适配器
private void initViewPager() {
// viewpager适配器
mOrderVpger.setAdapter(new MyOrderPagerAdapter(getSupportFragmentManager(), fragment_sell, tab_sell));
// 绑定TabLayout
mOrderTab.setupWithViewPager(mOrderVpger);
}
viewpager适配器
public class MyOrderPagerAdapter extends FragmentPagerAdapter {
private List<Fragment> fragment_sell;
private List<String> tab_sell;
public MyOrderPagerAdapter(FragmentManager fm, List<Fragment> fragment_sell, List<String> tab_sell) {
super(fm);
this.fragment_sell = fragment_sell;
this.tab_sell=tab_sell;
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return tab_sell.get(position);
}
@Override
public Fragment getItem(int position) {
return fragment_sell.get(position);
}
@Override
public int getCount() {
return fragment_sell.size();
}
}
// 自定义设置下划线长度
@Override
public void onStart() {
super.onStart();
mSyTab.post(new Runnable() {
@Override
public void run() {
setIndicator(mSyTab, 25, 25);
}
});
}
// 初始化下划线长度
public void setIndicator(TabLayout tabs, int leftDip, int rightDip) {
Class<?> tabLayout = tabs.getClass();
Field tabStrip = null;
try {
tabStrip = tabLayout.getDeclaredField("mTabStrip");
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
tabStrip.setAccessible(true);
LinearLayout llTab = null;
try {
llTab = (LinearLayout) tabStrip.get(tabs);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
int left = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, leftDip, Resources.getSystem().getDisplayMetrics());
int right = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, rightDip, Resources.getSystem().getDisplayMetrics());
for (int i = 0; i < llTab.getChildCount(); i++) {
View child = llTab.getChildAt(i);
child.setPadding(0, 0, 0, 0);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1);
params.leftMargin = left;
params.rightMargin = right;
child.setLayoutParams(params);
child.invalidate();
}
}
设置tablayout下划线长度的方法
public void setIndicator(TabLayout tabs, int leftDip, int rightDip) {
Class<?> tabLayout = tabs.getClass();
Field tabStrip = null;
try {
tabStrip = tabLayout.getDeclaredField("mTabStrip");
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
tabStrip.setAccessible(true);
LinearLayout llTab = null;
try {
llTab = (LinearLayout) tabStrip.get(tabs);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
int left = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, leftDip, Resources.getSystem().getDisplayMetrics());
int right = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, rightDip, Resources.getSystem().getDisplayMetrics());
for (int i = 0; i < llTab.getChildCount(); i++) {
View child = llTab.getChildAt(i);
child.setPadding(0, 0, 0, 0);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1);
params.leftMargin = left;
params.rightMargin = right;
child.setLayoutParams(params);
child.invalidate();
}
}
需要用的时候,,只要在有tablayout的地方就可以用了
使用的方法如下面的代码所示
这里面直接post了这个,并开了Runnable,然后设置当前的tablayout的对象,并设置长度就可以了
@Override
public void onStart() {
super.onStart();
tabLayout.post(new Runnable() {
@Override
public void run() {
setIndicator(tabLayout, 50, 50);
}
});
}
ScrollView嵌套ViewPager+Fragment时冲突问题解决办法
1.ViewPager 中的Fragment 不显示
给xml中ScrollView设置android:fillViewport=”true” 属性。通过这个就可以让ViewPager中的内容显示。
顾名思义,这个属性允许 NestedScrollView中的组件去充满它。
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/com_bg_gray_eeeeee"
android:descendantFocusability="blocksDescendants"
android:orientation="vertical">
<com.bigkoo.convenientbanner.ConvenientBanner
android:id="@+id/acty_main_cbanner"
android:layout_width="match_parent"
android:layout_height="@dimen/comm_dim_150" />
<android.support.design.widget.TabLayout
android:id="@+id/acty_main_tab"
android:layout_width="match_parent"
android:layout_height="@dimen/comm_dim_40"
android:layout_alignParentBottom="true"
app:tabBackground="@drawable/selector_tab_bg"
app:tabIndicatorHeight="0dp"
app:tabSelectedTextColor="@color/com_tv_white_ffffff"
app:tabTextAppearance="@style/tab_txtSize"
app:tabTextColor="@color/com_tv_black_333333" />
<android.support.v4.view.ViewPager
android:id="@+id/acty_main_vp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
2.上下拖动ViewPager 不能垂直方向滚动
最开始使用的默认的ViewPager,发现不能上下滑动,然后通过查找别人的解决办法,NestedScrollView嵌套ViewPager后,如果viewPager中的fragment高度太长,会发现滑动不了,需要自定义ViewPager,测量ViewPager的高度.
public class WrapContentHeightViewPager extends ViewPager {
public WrapContentHeightViewPager(Context context) {
super(context);
}
public WrapContentHeightViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int height = 0;
for (int i = 0; i < getChildCount(); i++) {
View child = getChildAt(i);
child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
int h = child.getMeasuredHeight();
if (h > height) height = h;
}
heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
然后将xml布局的ViewPager替换为我们自定义的ViewPager
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/com_bg_gray_eeeeee"
android:descendantFocusability="blocksDescendants"
android:orientation="vertical">
<com.bigkoo.convenientbanner.ConvenientBanner
android:id="@+id/acty_main_cbanner"
android:layout_width="match_parent"
android:layout_height="@dimen/comm_dim_150" />
<android.support.design.widget.TabLayout
android:id="@+id/acty_main_tab"
android:layout_width="match_parent"
android:layout_height="@dimen/comm_dim_40"
android:layout_alignParentBottom="true"
app:tabBackground="@drawable/selector_tab_bg"
app:tabIndicatorHeight="0dp"
app:tabSelectedTextColor="@color/com_tv_white_ffffff"
app:tabTextAppearance="@style/tab_txtSize"
app:tabTextColor="@color/com_tv_black_333333" />
<com.aoben.qproj.widget.WrapContentHeightViewPager
android:id="@+id/acty_main_vp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>