2015年的时候,google发布了新的Android Support Design库,里面包含了几个新的控件,其中就有一个TabLayout,它就可以完成TabPageIndicator的效果,而且还是官方的,所以有类是的效果还是推荐使用官方的。不过,也可以自己自定义来实现。
以前项目实战过~不足之处欢迎喷我~
项目效果图
TabLayout的使用
<?xml version="1.0" encoding="utf-8"?>
<com.eightbears.beboy.base.view.SwipeBackLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/sw_shopdetail"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/bg_f2_color"
android:orientation="vertical">
<include layout="@layout/title_common_title"/>
<android.support.design.widget.TabLayout
android:id="@+id/tl_shopbook_indicator"
style="@style/MyCustomTabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabTextAppearance="@style/MyCustomTabTextAppearance"
app:tabTextColor="#111111">
</android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:id="@+id/vp_shopbook"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="@dimen/dp_10"/>
</LinearLayout>
</com.eightbears.beboy.base.view.SwipeBackLayout>
Activity代码
TabLayout tl_shopbook_indicator;
@BindView(R.id.vp_shopbook)
ViewPager vp_shopbook;
@BindView(R.id.tv_title)
TextView tv_title;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_book);
initView();
initData();
initEvent();
}
@Override
protected void initView() {
ButterKnife.bind(this);
tv_title.setText(R.string.mybook);
vp_shopbook.setAdapter(new ShopBookViewPagerAdapter(mContext, getSupportFragmentManager()));
//绑定
tl_shopbook_indicator.setupWithViewPager(vp_shopbook);
}
Style
设置主题样色
Adapter
public class ShopBookViewPagerAdapter extends FragmentStatePagerAdapter {
private final String[] mTabNames;
public ShopBookViewPagerAdapter(Context context, FragmentManager fm) {
super(fm);
mTabNames = context.getResources().getStringArray(R.array.tab_name);
}
@Override
public Fragment getItem(int position) {
return FragmentFactory.createFragment(position);
}
@Override
public int getCount() {
return mTabNames.length;
}
@Override
public CharSequence getPageTitle(int position) {
return mTabNames[position];
}
}
public class FragmentFactory {
public static Fragment createFragment(int position) {
Fragment fragment = null;
switch (position) {
case 0:
fragment = new AllBookFragment();
break;
case 1:
fragment = new DisableBookFragment();
break;
case 2:
fragment = new WaitUseBookFragment();
break;
case 3:
fragment = new UsedBookFragment();
break;
}
return fragment;
}
}
TabLayout常用属性
当然还有很多,大家可以自行研究一下。。。
如果觉得我的文章对您有用,请点赞。您的支持将鼓励我继续码蛋!