Android——TabLayout——选项卡的便捷实现

个人博客CoorChice,https://chenbingx.github.io/ ,最新文章将会首发CoorChice的博客,欢迎探索哦 !
同时,搜索微信公众号CoorChice,或扫描文章末尾二维码,可以关注我的微信公众号。同期文章也将会优先推送到微信公众号中,以提醒您有新鲜文章出炉。

TabLayout可以方便的实现导航标签功能,它需要与ViewPager联合使用,ViewPager加载适配器,TabLayout加载ViewPager
使用前需要先导入desgin包

属性

app:tabIndicatorColor="@color/white"      // 下方滚动的下划线颜色  
app:tabSelectedTextColor="@color/gray"    // tab被选中后,文字的颜色  
app:tabTextColor="@color/white"           // tab默认的文字颜色  

几个关键方法

mTabLayout.setTabMode(TabLayout.MODE_FIXED); //设置TabLayout的标签模式

mTabLayout.addTab(); //为TabLayout添加标签

mTabLayout.setupWithViewPager(); //为TabLayout添加ViewPager

下面在实例中介绍

activity.xml



    
    
    
    



activity.java

public class MainActivity extends AppCompatActivity {

    private TabLayout mTabLayout;
    private ViewPager mViewPager;
    private String[] titles = {"最新","一周最热","买买买","读书","设计","文艺","礼物","指南","爱美"};
    private FragmentManager fragmentManager;
    private List fragments = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        fragmentManager = getSupportFragmentManager();
        for (int i = 0; i < titles.length; i++) {
            fragments.add(Fragment1.newInstance(""+i));
        }
        initView();
    }

    private void initView() {
        mTabLayout = (TabLayout) findViewById(R.id.tabLayout);
        mViewPager = (ViewPager) findViewById(R.id.viewPager);

        for (int i = 0; i < titles.length; i++) {
            //设置tab的标题
            mTabLayout.addTab(mTabLayout.newTab().setText(titles[i]));
        }

        mViewPager.setAdapter(new FragmentPagerAdapter(fragmentManager) {
            @Override
            public Fragment getItem(int position) {
                return fragments.get(position);
            }

            @Override
            public int getCount() {
                return fragments.size();
            }

            //使用Fragment需要指定好tab
            @Override
            public CharSequence getPageTitle(int position) {
                return titles[position % titles.length];
            }
        });

        //MODE_SCROLLABLE:可以滚动
        //MODE_FIXED:会适配到屏幕
        mTabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        //将ViewPager加载到TabLayout中,使它们能够联动
        mTabLayout.setupWithViewPager(mViewPager);
    }
}


fragment.java

public class Fragment1 extends Fragment {

    public static Fragment1 newInstance(String content) {
        Bundle args = new Bundle();
        args.putString("content",content);
        Fragment1 fragment = new Fragment1();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_tab,container,false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        ((TextView)(getView().findViewById(R.id.content))).setText(getArguments().getString("content"));
    }
}
Android——TabLayout——选项卡的便捷实现_第1张图片
效果
Android——TabLayout——选项卡的便捷实现_第2张图片
CoorChice的公众号

你可能感兴趣的:(Android——TabLayout——选项卡的便捷实现)