我如何使用bottomnavigationview从片段调用片段-Kotlin

问题描述 投票:0回答:1

我遇到了问题。

MainActivity中有5个片段。它是D1,D2,D3,D4,D5片段。 MainActivity中有BottomNavigationView。我想从D1fragment调用DxFragment。单击该按钮,仅框架会改变。但是按钮导航栏不会更改。

我已经找了一段时间。我找不到解决方案。谁能帮忙吗?

维护性MainActivity类:AppCompatActivity(){ 私人lateinit var bottomNavigationView:BottomNavigationView 重写fun onCreate(savedInstanceState:Bundle?){ super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) bottomNavigationView = findViewById(R.id.nav_view)

    navigateFragment(false)

}
public fun loadFragment (fragment: Fragment){
    supportFragmentManager.beginTransaction().also { fragmentTransaction ->
        fragmentTransaction.replace(R.id.bottom_nav_host_fragment, fragment)
        fragmentTransaction.commit()
    }
}

public fun navigateFragment ( stateFragment : Boolean) {
        bottomNavigationView = findViewById(R.id.nav_view)

        bottomNavigationView.setOnNavigationItemSelectedListener { menuItem : MenuItem ->
            when{
                menuItem.itemId== R.id.navigation_home -> {
                    loadFragment(D1Fragment())
                    return@setOnNavigationItemSelectedListener true
                }
                menuItem.itemId == R.id.navigation_map -> {
                    loadFragment(D2Fragment())
                    return@setOnNavigationItemSelectedListener true
                }
                menuItem.itemId == R.id.navigation_userpage -> {
                    loadFragment(D3Fragment())
                    return@setOnNavigationItemSelectedListener true
                }
                menuItem.itemId == R.id.navigation_fav -> {
                    loadFragment(D4Fragment())
                    return@setOnNavigationItemSelectedListener true
                }
                menuItem.itemId == R.id.navigation_list -> {
                    loadFragment(D5Fragment())
                    return@setOnNavigationItemSelectedListener true
                }
                stateFragment == true -> {
                    loadFragment(BlankFragment())
                    return@setOnNavigationItemSelectedListener true
                }

                else -> {
                    return@setOnNavigationItemSelectedListener true
                }

            }
        }


}

override fun onSupportNavigateUp(): Boolean {
    return Navigation.findNavController(this, R.id.bottom_nav_host_fragment).navigateUp() ||
            super.onSupportNavigateUp()
}

}

ACTIVITY_MAIN.XML

<com.google.android.material.bottomnavigation.BottomNavigationView
    android:id="@+id/nav_view"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="0dp"
    android:layout_marginEnd="0dp"
    android:background="?android:attr/windowBackground"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:menu="@menu/bottom_menu" />

<FrameLayout
    android:id="@+id/bottom_nav_host_fragment"
    android:name="androidx.navigation.fragment.NavHostFragment"
    android:layout_width="match_parent"
    android:layout_height="0dp"

    app:layout_constraintBottom_toTopOf="@id/nav_view"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    />

FRAGMENT_D1.XML

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



    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="CLICK AND GO BLANKFRAGMENT" />

</FrameLayout>

D1FRAGMENT

class D1Fragment : Fragment() {

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    // Inflate the layout for this fragment



    return inflater.inflate(R.layout.fragment_d1, container, false)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {



    button.setOnClickListener {

        Toast.makeText(context , "CLICKED" , Toast.LENGTH_SHORT ).show()
        Navigation.findNavController(view).navigate(R.id.action_d1Fragment_to_blankFragment)


    }
    super.onViewCreated(view, savedInstanceState)
}


}

enter image description here

android android-studio android-fragments kotlin bottomnavigationview
1个回答
0
投票

您只需要有一个班级,

示例主要活动,它包含BottomNavigationView和片段。片段具有其他元素

示例

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <FrameLayout
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>


    <BottomNavigationView
        android:id="@+id/bottom_navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:menu="@menu/bottom_nav_menu" />

</androidx.con straintlayout.widget.ConstraintLayout>

您只需要附加片段

© www.soinside.com 2019 - 2024. All rights reserved.