Android底部导航栏实现方式

Android底部导航栏实现

TabLayout+ViewPager+Fragment

https://blog.csdn.net/chengmuzhe2690/article/details/89406085

布局文件:

<android.support.v4.view.ViewPager
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:id="@+id/viewpager"></android.support.v4.view.ViewPager>
 
    <android.support.design.widget.TabLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        app:tabIndicatorHeight="0dp"
        app:tabSelectedTextColor="@android:color/holo_red_dark"
        app:tabTextColor="@android:color/black"
        android:id="@+id/tablayout">

Activity

private TabLayout tabLayout;
    private List<Fragment> fragments;
    private ViewPager viewPager;
    private FragmentPagerAdapter adapter;
    private String[] strings ={"Home","DashBoard","Notification","Email","Alert"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tablayout_test);
        initView();
    }
 
    private void initView() {
        tabLayout = findViewById(R.id.tablayout);
        fragments = new ArrayList<Fragment>();
        fragments.add(HomeFragment.newInstance());
        fragments.add(DashboardFragment.newInstance());
        fragments.add(NotificationFragment.newInstance());
        fragments.add(EmailFragment.newInstance());
        fragments.add(AlertFragment.newInstance());
        viewPager = findViewById(R.id.viewpager);
        adapter = new TitleFragmentPagerAdapter(getSupportFragmentManager(),fragments,strings);
        viewPager.setAdapter(adapter);
 
        tabLayout.setupWithViewPager(viewPager);
        onTabItemSelected();
    }
 
    private void onTabItemSelected() {
        for (int i =0;i<tabLayout.getTabCount();i++) {
            TabLayout.Tab tab = tabLayout.getTabAt(i);
            Drawable drawable = null;
            switch (i){
                case 0:
                    drawable = getResources().getDrawable(R.drawable.ic_home_black_24dp);
                    break;
 
                case 1:
                    drawable = getResources().getDrawable(R.drawable.ic_dashboard_black_24dp);
                    break;
                case 2:
                    drawable = getResources().getDrawable(R.drawable.ic_notifications_black_24dp);
                    break;
                case 3:
                    drawable = getResources().getDrawable(R.drawable.email);
                    break;
                case 4:
                    drawable = getResources().getDrawable(R.drawable.dialog_alert_icon);
                    break;
            }
            tab.setIcon(drawable);
        }
    }
 
 
    private class TitleFragmentPagerAdapter extends FragmentPagerAdapter {
 
 
        private List<Fragment> mFragmentList = null;
 
        private String[] titles;
 
        public TitleFragmentPagerAdapter(FragmentManager mFragmentManager,
                                         List<Fragment> fragmentList) {
            super(mFragmentManager);
            mFragmentList = fragmentList;
        }
 
 
        public TitleFragmentPagerAdapter(FragmentManager mFragmentManager,
                                         List<Fragment> fragmentList, String[] titles) {
            super(mFragmentManager);
            mFragmentList = fragmentList;
            this.titles = titles;
        }
 
        @Override
        public int getCount() {
            return mFragmentList.size();
        }
 
 
        @Override
        public Fragment getItem(int position) {
 
            Fragment fragment = null;
            if (position < mFragmentList.size()) {
                fragment = mFragmentList.get(position);
            } else {
                fragment = mFragmentList.get(0);
            }
            return fragment;
        }
 
        @Override
        public CharSequence getPageTitle(int position) {
            if (titles != null && titles.length > 0)
                return titles[position];
            return null;
        }
    }

Androdx控件:

BottomNavigationView+ViewPager+fragment

https://blog.csdn.net/qq_15396257/article/details/116498355

https://www.cnblogs.com/sunxun/p/9241180.html

https://blog.csdn.net/xiaoyangsavvy/article/details/70213537

https://github.com/brucevanfdm/BottomNavigationView

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值