本文介绍了如果使用视图绑定,如何找到NavController?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常,我在代码A中使用 val navController:NavController = findNavController(R.id.nav_host_fragment)查找NavController,它基于 R.id.nav_host_fragment ./p>

现在,我像代码B一样在应用程序中使用视图绑定,如果我使用视图绑定,如何使用NavController?

顺便说一句,在我看来 R.id.nav_host_fragment 在视图绑定中不可用,对吧?

代码A

  class TasksActivity:AppCompatActivity(){重写fun onCreate(savedInstanceState:Bundle?){super.onCreate(savedInstanceState)setContentView(R.layout.tasks_act)val navController:NavController = findNavController(R.id.nav_host_fragment)}} 

代码B

  class TasksActivity:AppCompatActivity(){私有lateinit var绑定:TasksActBinding重写fun onCreate(savedInstanceState:Bundle?){super.onCreate(savedInstanceState)绑定= TasksActBinding.inflate(layoutInflater)val view = binding.rootsetContentView(view)//val navController:NavController = findNavController(R.id.nav_host_fragment)}} 

tasks_act.xml

 < androidx.drawerlayout.widget.DrawerLayoutxmlns:android ="http://schemas.android.com/apk/res/android"xmlns:app =" http://schemas.android.com/apk/res-autoxmlns:tools ="http://schemas.android.com/tools"android:id =" @ + id/drawer_layout"android:layout_width =" match_parent"android:layout_height =" match_parent"工具:context =.tasks.TasksActivity";工具:openDrawer =开始"< LinearLayoutandroid:layout_width =" match_parent"android:layout_height =" match_parent"android:orientation =" vertical">< com.google.android.material.appbar.AppBarLayoutandroid:layout_width =" match_parent"android:layout_height =" wrap_content">< androidx.appcompat.widget.Toolbarandroid:id ="@@ id/toolbar"android:layout_width =" match_parent"android:layout_height =" wrap_content"android:minHeight =?attr/actionBarSize"android:theme ="@ style/Toolbar"app:popupTheme =" @ style/ThemeOverlay.AppCompat.Light"/></com.google.android.material.appbar.AppBarLayout><片段android:id =" @ + id/nav_host_fragment"android:name ="androidx.navigation.fragment.NavHostFragment"android:layout_width =" match_parent"android:layout_height =" match_parent"app:defaultNavHost =" true"app:navGraph =" @ navigation/nav_graph"/></LinearLayout>..</androidx.drawerlayout.widget.DrawerLayout> 
解决方案

我们正在使用ViewBinding获取视图本身的引用.如果在TextView中使用视图绑定,则将获得View的引用.

对于NavCotroller,如果使用Viewbinding,则将获得对片段的引用,而 findNavController 则期望ViewId为整数类型.

  findNavController(@IdRes viewId:int) 

Normally, I use val navController: NavController = findNavController(R.id.nav_host_fragment) in Code A to find NavController, it's based R.id.nav_host_fragment.

Now I use view binding in the app just like Code B, how can I NavController if I use view binding ?

BTW, in my mind R.id.nav_host_fragment will not be available in view binding , right?

Code A

class TasksActivity : AppCompatActivity() {

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

        val navController: NavController = findNavController(R.id.nav_host_fragment)
     }

}

Code B

class TasksActivity : AppCompatActivity() {

    private lateinit var binding: TasksActBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = TasksActBinding.inflate(layoutInflater)
        val view = binding.root
        setContentView(view)

        //val navController: NavController = findNavController(R.id.nav_host_fragment)
    }

}

tasks_act.xml

<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"
    tools:context=".tasks.TasksActivity"
    tools:openDrawer="start">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <com.google.android.material.appbar.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:minHeight="?attr/actionBarSize"
                android:theme="@style/Toolbar"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
        </com.google.android.material.appbar.AppBarLayout>

        <fragment
            android:id="@+id/nav_host_fragment"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"

            app:defaultNavHost="true"
            app:navGraph="@navigation/nav_graph" />

    </LinearLayout>

    ..

</androidx.drawerlayout.widget.DrawerLayout>
解决方案

We are using ViewBinding to get a reference of view itself. If you are using viewbinding in TextView, you will get a reference of the View.

In the case of NavCotroller, If you use Viewbinding, you will get a reference to a fragment, while findNavController expect ViewId of type integer.

findNavController(@IdRes viewId: int)

这篇关于如果使用视图绑定,如何找到NavController?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-09 08:03