Android垂直tab导航栏、左侧竖直tab导航栏,支持viewpager,不依赖viewpager

人生导言:   做你害怕做的事情,然后你发现,不过如此。
这是基于有时候需要左侧竖直tab导航栏 ,可以直接拿来用,继承自ScrollView      

    先来看下效果

Android垂直tab导航栏、左侧竖直tab导航栏,支持viewpager,不依赖viewpager_第1张图片

部分代码:

适配器

 private class MyTabAdapter implements TabAdapter {

        List menus;

        public MyTabAdapter() {
            menus = new ArrayList<>();
            Collections.addAll(menus, "骆驼", "凉凉", "侧脸", "尽头", "流浪",
                    "过客", "湮灭");
        }

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

        @Override
        public TabView.TabBadge getBadge(int position) {
            return null;
        }

        @Override
        public TabView.TabIcon getIcon(int position) {
            return null;
        }

        @Override
        public TabView.TabTitle getTitle(int position) {
            return new TabView.TabTitle.Builder()
                    .setContent(menus.get(position))
                    .setTextColor(0xFFFFFFFF, 0xFF333333)//可自己设置tab字体颜色
                    .build();
        }

        @Override
        public int getBackground(int position) {
            return -1;
        }

    }

当然也支持viewpager

  private class MyPagerAdapter extends PagerAdapter implements TabAdapter {
        List titles;

        public MyPagerAdapter() {
            titles = new ArrayList<>();
            Collections.addAll(titles, "骆驼", "凉凉", "侧脸", "尽头", "流浪",
                    "过客", "湮灭");
        }

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

        @Override
        public TabView.TabBadge getBadge(int position) {

            return null;
        }

        @Override
        public TabView.TabIcon getIcon(int position) {
            return null;
        }

        @Override
        public TabView.TabTitle getTitle(int position) {

            return new TabView.TabTitle.Builder()
                    .setContent(titles.get(position))
                    .setTextColor(Color.BLACK, Color.BLACK)
                    .build();
        }

        @Override
        public int getBackground(int position) {
            return 0;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            TextView tv = new TextView(MainActivity.this);
            tv.setTextColor(Color.BLACK);
            tv.setGravity(Gravity.CENTER);
            tv.setText(titles.get(position));
            tv.setTextSize(18);
            container.addView(tv);
            return tv;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    }

然后调用

  private void initTab0() {
        final VerticalTabLayout tablayout = (VerticalTabLayout) findViewById(R.id.tablayout2);
        viewpager.setAdapter(new MyPagerAdapter());
        tablayout.setupWithViewPager(viewpager);
    }

    private void initTab() {
        VerticalTabLayout tablayout =findViewById(R.id.tablayout2);
        tablayout.setTabAdapter(new MyTabAdapter());
        tablayout.addOnTabSelectedListener(new VerticalTabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabView tab, int position) {

            }

            @Override
            public void onTabReselected(TabView tab, int position) {

            }
        });
    }

代码demo传送门

你可能感兴趣的:(android)