android - 抽屉导航用另一个有问题的保留抽屉选项的 fragment 替换 fragment

标签 android xml android-layout android-fragments kotlin

我有一个基本的应用程序,包含抽屉布局。抽屉中的每个选项都会打开一个新的 fragment 页面供用户查看。这是抽屉布局代码。

<androidx.drawerlayout.widget.DrawerLayout
    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:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />

</androidx.drawerlayout.widget.DrawerLayout>

这是 app_bar_main.xml 代码。

<androidx.coordinatorlayout.widget.CoordinatorLayout 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=".DashBoardActivity">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

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

    <include layout="@layout/content_main" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

这是 content_main.xml。还有主要 Activity DashBoardActivity.kt

class DashBoardActivity : AppCompatActivity() {

    private lateinit var appBarConfiguration: AppBarConfiguration

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_dashboard)

        val toolbar: Toolbar = findViewById(R.id.toolbar)
        setSupportActionBar(toolbar)

        val drawerLayout: DrawerLayout = findViewById(R.id.drawer_layout)
        val navView: NavigationView = findViewById(R.id.nav_view)
        val navController = findNavController(R.id.nav_host_fragment)
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        appBarConfiguration = AppBarConfiguration(
            setOf(
                R.id.nav_company_emissions, R.id.nav_your_emissions, R.id.nav_contact,
                R.id.nav_privacy_policy
            ), drawerLayout
        )
        setupActionBarWithNavController(navController, appBarConfiguration)
        navView.setupWithNavController(navController)
    }

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        // Inflate the menu; this adds items to the action bar if it is present.
        menuInflater.inflate(R.menu.main, menu)
        return true
    }

    override fun onSupportNavigateUp(): Boolean {
        val navController = findNavController(R.id.nav_host_fragment)
        return navController.navigateUp(appBarConfiguration) || super.onSupportNavigateUp()
    }
}

根据我的理解,当您单击抽屉布局中的按钮时,主机 fragment 中的 fragment 将替换为相关的单击 fragment 。

我的问题是...如何以编程方式将另一个 fragment 放入尚未使用抽屉布局设置的导航主机 View 中。我的问题是其中一个 fragment 包含带有编辑按钮的用户的配置文件。我想在单击编辑按钮时打开一个新 fragment ,并希望用户在不想进入另一个 Activity 时仍然可以访问抽屉。

在我看来,我只想用 edit_fragment 替换 profile_fragment。但是,当我尝试下面类似的操作时,它会将 fragment 放置在配置文件的一个上,这样您就可以看到这个看起来很奇怪的屏幕,其中两个 fragment 彼此叠置。

val frag: Fragment = EditYourEmissionsFragment()
val fragMan:FragmentManager = activity!!.supportFragmentManager
val fragTran: FragmentTransaction = fragMan.beginTransaction()
fragTran.add(R.id.nav_host_fragment, frag)
fragTran.addToBackStack(null)
fragTran.commit()

对于 Android 和 Kotlin 来说,我还是一个相对新手,所以我很难理解我将如何处理我的用户案例。

尝试帮助描绘我想要发生的事情的场景:

  1. 用户加载应用并登陆 home_fragment。
  2. 打开抽屉式菜单,点击 profile_fragment。
  3. 打开 profile_fragment 后,用户按下编辑键。
  4. 新的 fragment 接管屏幕,但保留打开菜单抽屉的功能。
  5. 用户现在可以打开抽屉式菜单并导航到应用程序中的其他位置。或者保存他们的个人资料并返回到 profile_fragment。

预先感谢您的帮助。如果不是特别清楚,请道歉。

最佳答案

在导航图中包含 EditYourEmissionsFragment,如下所示:

    <fragment
        android:id="@+id/EditYourEmissionsFragment"
        android:name="com.example.application.EditYourEmissionsFragment"
        android:label="Edit Profile"
        tools:layout="@layout/fragment_edit_omissions"/>

然后在您的 ProfileFragment 上设置 onClick 以像这样打开目的地;

        val navController = Navigation.findNavController(view)
        navController.navigate(R.id.EditYourEmissionsFragment)

并在您的 MainActivity 中

    override fun onSupportNavigateUp(): Boolean {
        val navController = findNavController(R.id.my_nav_host)
        return navController.navigateUp(myAppBarConfiguration) || super.onSupportNavigateUp()
    }

关于android - 抽屉导航用另一个有问题的保留抽屉选项的 fragment 替换 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59983931/

相关文章:

android - 在安卓手机上设置相机分辨率

android - 从 Rails 应用程序向 iOS、Android、BlackBerry 发送通知

android - MotionLayout 与垂直 ScrollView

android - 在 Android 中显示为按钮背景的错误图像

java - onKeyDown 事件的范围变量问题

java - 使用JRuby直接调用Android Java方法

xml - 如何将 XmlNode 从一个 XmlDocument 复制到另一个?

javascript - 统计一个XML文件中有多少父节点有相同的Tag's 'value'

java - 来自 org.w3c.dom 的 getElementsByTagName 未获取所有子节点

android - 我的相机 View 与 GLSurfaceView 重叠...但我希望它被 GLSurfaceView 覆盖