Android中竖着的Tablayout的简单使用

视图效果

db5104bce737ea0c63a34106cf1c197.png

1.添加依赖

implementation 'q.rorbin:VerticalTabLayout:1.2.5'

2.XML布局的使用

`









3.Java中的书写

从网络上获取数据进行解析,然后进行适配器的书写

public class CategoryFragment extends BaseFragment implements CategoryContract.View {

    @BindView(R.id.vTab)
    VerticalTabLayout vTab;
    @BindView(R.id.vp_sort)
    ViewPager vpSort;
    Unbinder unbinder;


    @Override
    protected int getLayout() {
        return R.layout.fragment_category;
    }

    @Override
    protected void initView(View view) {

    }

    @Override
    protected void initData() {
        ((CategoryPresenter)presenter).category();
    }

    @Override
    protected IPresenter createPresenter() {
        return new CategoryPresenter();
    }

    @Override
    public void showErrMsg(String err) {
        Toast.makeText(getActivity(), err, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void categoryDataReturn(List categoryList) {
        final ArrayList tabName = new ArrayList<>();
        final ArrayList fragments = new ArrayList<>();

        for (int i = 0; i < categoryList.size(); i++) {
            fragments.add(new CategoryTabFragment(categoryList.get(i).getName(), categoryList.get(i).getId() + ""));
            tabName.add(categoryList.get(i).getName());
        }
        FragTabAdapter fragTabAdapter = new FragTabAdapter(getChildFragmentManager(), fragments,
                tabName);
        vpSort.setAdapter(fragTabAdapter);
        vTab.setupWithViewPager(vpSort);
    }

}

FragTabAdapter的书写

public class FragTabAdapter extends FragmentPagerAdapter {
    private ArrayList fragments = new ArrayList<>();
    private ArrayList tabName = new ArrayList<>();

    public FragTabAdapter(FragmentManager fm, ArrayList fragments, ArrayList
            tabName) {
        super(fm);
        this.fragments = fragments;
        this.tabName = tabName;
    }

    @Override
    public Fragment getItem(int i) {
        return fragments.get(i);
    }

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

    @Nullable
    @Override
    public CharSequence getPageTitle(int position) {
        return tabName.get(position);
    }
}

你可能感兴趣的:(Android中竖着的Tablayout的简单使用)