【问题标题】:The Home fragment rises above the restHome 片段高于其他片段
【发布时间】:2023-01-14 03:24:55
【问题描述】:

当我在我创建的片段之间切换时,主片段不会被删除,它会写在其他片段上。

请我有一个问题。我正在使用底部导航和导航控制器。一旦我切换片段,主页片段就会一直显示在其余部分下面

`
class FeedActivity : AppCompatActivity() {

    private lateinit var bottomNavigationView: BottomNavigationView


 //loadFragment(HomeFragment())
        bottomNavigationView = findViewById(R.id.bottom_navigation) as BottomNavigationView
        bottomNavigationView.setOnItemSelectedListener {
            when (it.itemId) {
                R.id.nav_home -> {
                    loadFragment(HomeFragment())
                    true
                }
                R.id.nav_search -> {
                    loadFragment(SearchFragment())
                    true
                }

                R.id.nav_add -> {
                    loadFragment(AddFragment())
                    true
                }
                R.id.nav_notifications -> {
                    loadFragment(NotificationsFragment())
                    true
                }
                R.id.nav_profile -> {
                    loadFragment(ProfileFragment())
                    true
                }
                else -> throw IllegalStateException("Someone forgot to add enough fragment cases to 'when' clause!")
            }
        }


        }
    private  fun loadFragment(fragment: Fragment){
        val transaction = supportFragmentManager.beginTransaction()
        transaction.replace(R.id.cercevekapsayici,fragment)
        transaction.commit()
    }`

`<RelativeLayout
    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=".view.FeedActivity">

    <FrameLayout
        android:id="@+id/cercevekapsayici"
        android:layout_above="@id/bottom_appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/bottom_appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" >

        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/bottom_navigation"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?android:attr/windowBackground"
            app:itemIconTint="@color/cardview_dark_background"
            app:labelVisibilityMode="unlabeled"
            app:menu="@menu/bottom_navigation" />

    </com.google.android.material.appbar.AppBarLayout>

[[enter image description here](https://i.stack.imgur.com/3D2Z8.png)](https://i.stack.imgur.com/llwZ5.png)`

当我切换到其他片段时,我希望 recyclerview 消失。

【问题讨论】:

    标签: android kotlin android-recyclerview fragment android-bottomnav


    【解决方案1】:

    你可以让你的 Farm Layout 高度匹配 parent :

        <FrameLayout
           android:id="@+id/cercevekapsayici"
           android:layout_above="@id/bottom_appbar"
           android:layout_width="match_parent"
           android:layout_height="match_parent" />
    

    【讨论】:

      【解决方案2】:

      您的 loadFragment 函数替换了附加到 R.id.cercevekapsayici 的任何 Fragment,后者是包含您的片段的 FrameLayout。但是你的RecyclerView是你主布局的一部分,所以无论你附加到R.id.cercevekapsayici,它都会一直在那里。

      如果你想替换它,把它放在 Fragment 中,然后将它添加到 FrameLayout - 然后当你切换到不同的片段时它会被删除。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-10-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多