android 顶部导航栏

1、依赖 implementation ‘com.android.support:design:28.+’//顶部导航栏

2、布局文件



    

3、activity中初始化:


		private TabLayout tableLayout;
		private ViewPager viewPager;
		tableLayout = findViewById(R.id.order_tab);
		viewPager = findViewById(R.id.order_viewpager);
		
		List fragments = new ArrayList<>();
        fragments.add(new OrderAllFragment());
        fragments.add(new WaitPayFragment());
        fragments.add(new WaitGoodsFragment());
        fragments.add(new GetGoodsFragment());
            FragmentPagerAdapter adapter = new FragmentOrderListAdapter(getSupportFragmentManager(),fragments, new String[]{"全部", "待付款", "待收货","已收货"});
        viewPager.setAdapter(adapter);
        tableLayout.setupWithViewPager(viewPager);

OrderAllFragment、WaitPayFragment、WaitGoodsFragment、GetGoodsFragment为自己写的fragment

4、FragmentOrderListAdapter


public class FragmentOrderListAdapter extends FragmentPagerAdapter {
    private List list;
    private String[] titles;
    private Context context;
    public FragmentOrderListAdapter(FragmentManager mFragmentManager,
                                     List fragmentList,String[] title) {
        super(mFragmentManager);
        list = fragmentList;
        titles=title;
    }
    @Override
    public Fragment getItem(int i) {
        Fragment fragment = null;
        if (i < list.size()) {
            fragment = list.get(i);
        } else {
            fragment = list.get(0);
        }
        return fragment;
    }

    @Override
    public int getCount() {
        return list.size();
    }
    @Override
    public CharSequence getPageTitle(int position) {
        if (titles != null && titles.length > 0)
            return titles[position];
        return null;
    }
}


结束,

你可能感兴趣的:(android 顶部导航栏)