Android 6.0 TabLayout简单使用

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常用属性

这里写图片描述

当然还有很多,大家可以自行研究一下。。。

如果觉得我的文章对您有用,请点赞。您的支持将鼓励我继续码蛋!

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值