欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Android中竖着的Tablayout的简单使用

程序员文章站 2024-03-20 19:43:10
...

                Android中竖着的Tablayout的简单使用

1.添加依赖

implementation 'q.rorbin:VerticalTabLayout:1.2.5'

2.XML布局的使用

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.douwenxuan.view.category.CategoryFragment">

<q.rorbin.verticaltablayout.VerticalTabLayout
    android:id="@+id/vTab"
    android:layout_alignParentLeft="true"
    android:layout_width="80dp"
    android:layout_height="match_parent"
    android:background="@color/white"/>
<android.support.v4.view.ViewPager
    android:id="@+id/vp_sort"
    android:layout_toRightOf="@id/vTab"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

</RelativeLayout>

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<CategoryListBean> categoryList) {
        final ArrayList<String> tabName = new ArrayList<>();
        final ArrayList<Fragment> 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<Fragment> fragments = new ArrayList<>();
    private ArrayList<String> tabName = new ArrayList<>();

    public FragTabAdapter(FragmentManager fm, ArrayList<Fragment> fragments, ArrayList<String>
            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);
    }
}