MainActivity内导航主机片段上的调用方法

时间:2020-01-29 10:41:42

标签: android android-fragments android-jetpack android-jetpack-navigation

我正在开发我的第一个Android应用程序,我知道架构可能有点混乱。 我在android中使用新的Jetpack导航。我有一个导航抽屉,在其中添加了回收站视图,用户可以从中选择回收站视图中的项目,并基于该主导航主机片段应滚动到特定位置。

我想知道如何在与导航主机片段关联的片段下面调用方法。我可以获取导航主机片段的句柄,但我想在该特定片段中调用自定义方法,该特定方法不会通过导航主机片段公开,因此它给了我编译时错误。

我正在使用的方法如下。

val readerFragment: ReaderFragment = supportFragmentManager.findFragmentById(R.id.reader_fragment) as ReaderFragment

readerFragment.scrollToChapter(5)

“阅读器”片段是导航主机片段下方使用的实际片段,这会导致以下异常

com.example.quran E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.quran, PID: 7640
    kotlin.TypeCastException: null cannot be cast to non-null type com.example.quran.ReaderFragment

或尝试像这样直接从nav_host_fragment获取

val readerFragment: ReaderFragment = nav_host_fragment as ReaderFragment

然后我得到以下异常

java.lang.ClassCastException: androidx.navigation.fragment.NavHostFragment cannot be cast to com.example.quran.ReaderFragment

有人可以帮我怎样从MainActivity的nav_host _fragment中获取实际片段。

下面是nav_graph的代码

<?xml version="1.0" encoding="utf-8"?>
<navigation 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/nav_graph"
    app:startDestination="@id/reader_fragment">
    <fragment
        android:id="@+id/reader_fragment"
        android:name="com.example.quran.ReaderFragment"
        android:label="Reader"
        tools:layout="@layout/reader_fragment">
    </fragment>
    <fragment
        android:id="@+id/chapterDetailsFragment"
        android:name="com.example.quran.ChapterDetailsFragment"
        android:label="fragment_chapter_details"
        tools:layout="@layout/fragment_chapter_details" />
</navigation>

这是MainActivity的布局

<?xml version="1.0" encoding="utf-8"?>
<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:context=".MainActivity"
    tools:openDrawer="start">

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

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorTopNavigation">

            <TextView
                android:id="@+id/toolbar_textView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fontFamily="@font/montserratbold"
                android:textColor="@color/toolbarItemColor"
                android:layout_marginRight="?android:attr/actionBarSize"
                android:gravity="center"
                android:textSize="30dp"
                android:textStyle="bold" />

        </androidx.appcompat.widget.Toolbar>

        <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>

    <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">


        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/chapters_recycler_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </com.google.android.material.navigation.NavigationView>


</androidx.drawerlayout.widget.DrawerLayout>

1 个答案:

答案 0 :(得分:1)

您的问题的说明

代码中的第一个错误:

 val readerFragment: ReaderFragment = nav_host_fragment as ReaderFragment

因为NavHostFragment是一个主机(即您的片段在其中),所以您无法将NavHostFragment投射到其他任何主机上。

 supportFragmentManager.findFragmentById(R.id.reader_fragment) as ReaderFragment

TypeCastException:无法将null强制转换为非null类型

findFragmentById返回null,因为它找不到您的片段。这是第二个错误。


解决方案

您可以对其稍加修改,以获取屏幕上的片段:

NavHostFragment navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host);
navHostFragment.getChildFragmentManager().getFragments().get(0);

您还可以检查子片段管理器,因为您的片段是导航主机的子片段:

NavHostFragment fragment = supportFragmentManager.findFragmentById(R.id.nav_host);
MyFragment frag = (MyFragment) fragment.getChildFragmentManager().findFragmentByTag(tag);

参考:

Android Navigation Architecture Component - Get current visible fragment

相关问题