当前位置:网站首页>Kotlin uses viewpager2+fragment+bottomnavigationview to implement the style of the switching module of the bottom menu bar.

Kotlin uses viewpager2+fragment+bottomnavigationview to implement the style of the switching module of the bottom menu bar.

2022-06-26 03:43:00 yyxhzdm

design sketch :

Implementation steps :

1. First, in the app Of builde Reference in file ViewPager2

implementation 'androidx.viewpager2:viewpager2:1.0.0-alpha01'

2. Add layout of first page xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.viewpager2.widget.ViewPager2
        android:id="@+id/mainViewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/navigationView" />

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/navigationView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="#FFFFFF" />

</RelativeLayout>

3. establish home Activity page

package com.example.tasklayoutdemo

import android.content.Context
import android.content.Intent
import androidx.fragment.app.Fragment
import androidx.viewpager2.widget.ViewPager2
import com.example.tasklayoutdemo.base.BaseAppActivity
import com.example.tasklayoutdemo.fragment.*
import com.google.android.material.bottomnavigation.LabelVisibilityMode
import kotlinx.android.synthetic.main.home_activity.*

class Home : BaseAppActivity() {

    companion object {
        fun startHomeActivity(context: Context) {
            context.startActivity(Intent(context, Home::class.java))
        }

        // Pass parameters 
        fun startHomeActivity2(
            context: Context,
            userid: String,
            token: String
        ) {
            context.startActivity(Intent(context, Home::class.java).apply {
                putExtra("UserId", userid)
                putExtra("Token", token)
            })
        }

    }

    override fun getLayout(): Int {
        return R.layout.home_activity
    }

    override fun initView() {


    }

    override fun initDate() {
        val fragmentArr = ArrayList<Fragment>()
        fragmentArr.add(ShoeYeFragment.instance)
        fragmentArr.add(InfoFragment.instance)
        fragmentArr.add(FindFragment.instance)
        fragmentArr.add(MineFragment.instance)

        navigationView.menu.add(0, 0, 1, " home page ").setIcon(R.drawable.tab_1)
        navigationView.menu.add(0, 1, 1, " news ").setIcon(R.drawable.tab_2)
        navigationView.menu.add(0, 2, 1, " garden plot ").setIcon(R.drawable.tab_4)
        navigationView.menu.add(0, 3, 1, " my ").setIcon(R.drawable.tab_3)
        /**
         *  No prevents the user from sliding the page 
         * */
        mainViewPager.isUserInputEnabled = false
        /**
         *  Set up ViewPager2 Sliding listening events for 
         * isUserInputEnabled = true  when 
         * */
        mainViewPager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                // Set the selected position of the navigation bar 
                navigationView.menu.getItem(position).setChecked(true)
            }
        })

        mainViewPager.adapter = PagerAdapter(this, fragmentArr)
        navigationView.labelVisibilityMode = LabelVisibilityMode.LABEL_VISIBILITY_LABELED
        /**
         *  Set navigation bar menu items Item Select monitor 
         * */
        navigationView.setOnNavigationItemSelectedListener {
            mainViewPager.currentItem = it.itemId
            true
        }

    }
}

4. Create the corresponding Fragment page ( In the example, you need to create four , I posted a code , The others are the same )

package com.example.tasklayoutdemo.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.example.tasklayoutdemo.R
import kotlinx.android.synthetic.main.shouye_fragment.*

class ShoeYeFragment : Fragment() {

    companion object {
        val instance: ShoeYeFragment by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
            ShoeYeFragment()
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.shouye_fragment, container, false)
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        tv_content.text = " I am a Fragment, home page ...."

    }

}

Fragment Corresponding layout file

........

5. establish PagerAdapter 

package com.example.tasklayoutdemo.fragment

import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.viewpager2.adapter.FragmentStateAdapter

class PagerAdapter : FragmentStateAdapter {
    private var fragments: List<Fragment>

    constructor(fragmentActivity: FragmentActivity, fragments: List<Fragment>) : super(
        fragmentActivity
    ) {
        this.fragments = fragments
    }

    constructor(fragment: Fragment, fragments: List<Fragment>) : super(
        fragment
    ) {
        this.fragments = fragments
    }

    override fun getItemCount(): Int {
        return fragments.size
    }

    override fun createFragment(position: Int): Fragment {
        return fragments[position]
    }

}

6. Add navigation bar icon

complete , Can run

7. Advanced

among app:itemTextColor="@drawable/main_bottom" Select the color of the font when the setting button is checked and unchecked

app:itemIconTint="@drawable/main_bottom" Select the color of the icon when the setting button is checked and unchecked

main_bottom.xml File for :

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@android:color/holo_red_light" android:state_checked="true" />
    <item android:color="@color/black" android:state_checked="false" />
</selector>
原网站

版权声明
本文为[yyxhzdm]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202180546432054.html