Android顶部导航

安卓顶部导航使用的是TabLayout和ViewPager的结合,布局代码如下:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/shape_gradient">

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="30dp"
            android:background="@drawable/shape_gradient"
            app:tabIndicatorColor="#2b86c5"
            app:tabSelectedTextColor="#FFFFFF" />
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>

实现代码,Fragment不懂得可以自己了解:

private List<Fragment> fragList= new ArrayList<>();
//每个页面的fragment页面
        fragList.add(new SecondFragmentFirst());
        fragList.add(new SecondFragmentSecond());
        fragList.add(new SecondFragmentThird());
        TabLayout tabs = view.findViewById(R.id.tabs);
        ViewPager viewPager = view.findViewById(R.id.view_pager);
        //设置最大缓存界面
        viewPager.setOffscreenPageLimit(3);
        viewPager.setAdapter(new ViewPageAdapter(getChildFragmentManager()));
        //将viewpage添加到TabLayout中
        tabs.setupWithViewPager(viewPager);
        for (int i = 0; i < tabs.getTabCount(); i++) {
     
            TabLayout.Tab tab = tabs.getTabAt(i);
            if (tab != null) {
     
                tab.setCustomView(getTabView(i));
            }
        }

        tabs.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
     
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
     
                //在这里可以设置选中状态下  tab字体显示样式
                viewPager.setCurrentItem(tab.getPosition());
                View view = tab.getCustomView();
                //判断其左边对象是否为其右边类的实例
                if (view instanceof TextView) {
     
                    ((TextView) view).setTextSize(19);
                    ((TextView) view).setTextColor(ContextCompat.getColor(getActivity(), R.color.orange));
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
     
                View view = tab.getCustomView();
                if (view instanceof TextView) {
     
                    ((TextView) view).setTextSize(14);
                    ((TextView) view).setTextColor(ContextCompat.getColor(getActivity(), R.color.white));
                }
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
     

            }
        });
        return view;
    }

    /**
     * 自定义Tab的View
     *
     * @param currentPosition
     * @return
     */
    private View getTabView(int currentPosition) {
     
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.public_tab_iterm, null);
        TextView textView = view.findViewById(R.id.tab_item_textview);
        textView.setText(titleList.get(currentPosition));
        return view;
    }

    class ViewPageAdapter extends FragmentPagerAdapter {
     

        public ViewPageAdapter(FragmentManager fm) {
     
            super(fm);
        }

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

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

你可能感兴趣的:(andriod,Android)