美文网首页安卓开发安卓开发Android开发经验谈
底部导航栏BottomNavigationView+ViewPa

底部导航栏BottomNavigationView+ViewPa

作者: 蓝不蓝编程 | 来源:发表于2019-05-15 10:01 被阅读12次

    实现效果:

    实现方案

    1. 主界面xml(包含viewPager和BottomNavigationView)
    <RelativeLayout 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">
    
        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/bottomNavigationView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            app:itemBackground="@color/colorPrimary"
            app:itemIconTint="@color/bnv_tab_item_foreground"
            app:itemTextColor="@color/bnv_tab_item_foreground"
            app:menu="@menu/bottom_navigation_main" />
    
        <androidx.viewpager2.widget.ViewPager2
            android:id="@+id/vp"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@id/bottomNavigationView" />
    </RelativeLayout>
    
    1. 底部菜单:bottom_navigation_main.xml
    <menu xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">
        <item
            android:id="@+id/action_favorites"
            android:enabled="true"
            android:icon="@drawable/ic_favorite_white_24dp"
            android:title="@string/favorites"
            app:showAsAction="ifRoom" />
        <item
            android:id="@+id/action_schedules"
            android:enabled="true"
            android:icon="@drawable/ic_access_time_white_24dp"
            android:title="@string/schedules"
            app:showAsAction="ifRoom" />
        <item
            android:id="@+id/action_music"
            android:enabled="true"
            android:icon="@drawable/ic_audiotrack_white_24dp"
            android:title="@string/mine"
            app:showAsAction="ifRoom" />
    </menu>
    
    1. MainActivity
    class MainActivity : AppCompatActivity() {
    
        private val favoritesTabIndex = 0
        private val schedulesTabIndex = 1
        private val mineTabIndex = 2
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
    
            val fragments = getFragments()
    
            vp.adapter = object : FragmentStateAdapter(this) {
                override fun getItem(position: Int): Fragment {
                    return fragments[position]
                }
    
                override fun getItemCount(): Int {
                    return fragments.size
                }
            }
            //禁用左右滑动切换页签
            vp.isUserInputEnabled = false
    
            bottomNavigationView.setOnNavigationItemSelectedListener { item ->
                when (item.itemId) {
                    R.id.action_favorites -> {
                        vp.setCurrentItem(favoritesTabIndex, false)
                    }
                    R.id.action_schedules -> {
                        vp.setCurrentItem(schedulesTabIndex, false)
                    }
                    R.id.action_music -> {
                        vp.setCurrentItem(mineTabIndex, false)
                    }
                }
                true
            }
        }
    
        private fun getFragments(): ArrayList<Fragment> {
            val fragments = ArrayList<Fragment>(3)
    
            val favoritesFragment = BaseFragment()
            var bundle = Bundle()
            bundle.putString("title", getString(R.string.favorites))
            favoritesFragment.arguments = bundle
    
            val schedulesFragment = BaseFragment()
            bundle = Bundle()
            bundle.putString("title", getString(R.string.schedules))
            schedulesFragment.arguments = bundle
    
            val mineFragment = BaseFragment()
            bundle = Bundle()
            bundle.putString("title", getString(R.string.mine))
            mineFragment.arguments = bundle
    
            fragments.add(favoritesFragment)
            fragments.add(schedulesFragment)
            fragments.add(mineFragment)
            return fragments
        }
    }
    

    源代码:

    https://github.com/cxyzy1/bottomNavigationViewSample.git

    安卓开发技术分享: https://www.jianshu.com/p/442339952f26
    点击关注专辑,查看最新技术分享
    更多技术总结好文,请关注:「程序园中猿」

    相关文章

      网友评论

        本文标题:底部导航栏BottomNavigationView+ViewPa

        本文链接:https://www.haomeiwen.com/subject/ntjuaqtx.html