Вызов метода для фрагмента узла навигации внутри MainActivity - PullRequest
0 голосов
/ 29 января 2020

Я занимаюсь разработкой своего первого Android приложения и знаю, что архитектура может быть немного грязной. Я использую новую навигацию Jetpack в android. У меня есть ящик навигации, в который я добавил представление рециркулятора, откуда пользователь может выбрать элемент в представлении рециркулятора, и на основе этого основного фрагмента хоста навигации следует прокрутить до указанного c местоположения.

Я хотел знаю, как я могу вызвать метод в нижнем фрагменте, связанном с фрагментом узла навигации. Я могу получить дескриптор фрагмента хоста навигации, но я хотел вызвать пользовательский метод в этом конкретном фрагменте, который не отображается через фрагмент хоста навигации, и из-за этого он дает мне ошибку во время компиляции.

Подход, который я использую, заключается в следующем.

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

readerFragment.scrollToChapter (5)

Здесь фрагмент Reader - это фактический фрагмент, используемый под фрагментом узла навигации, это приводит к следующему исключению

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

Может ли кто-нибудь помочь мне, как я могу получить фактический фрагмент из nav_host _fragment внутри MainActivity.

Ниже приведен код для 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 голосов
/ 29 января 2020

Объяснение ваших проблем

Ваша первая ошибка из кода:

 val readerFragment: ReaderFragment = nav_host_fragment as ReaderFragment

Это потому, что NavHostFragment является хостом (т.е. ваш фрагмент находится внутри это), так что вы не можете привести NavHostFragment ни к чему другому.

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

TypeCastException: ноль не может быть приведен к непустому типу

findFragmentById возвращает ноль, поскольку не находит ваш фрагмент. это вторая ошибка.


Решения

Вы можете немного взломать его, чтобы получить фрагмент экрана:

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

Вы также может проверить дочерний менеджер фрагментов, поскольку ваш фрагмент является дочерним по отношению к хосту nav:

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

Ссылка:

Android Компонент архитектуры навигации - получение текущего видимого фрагмента

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...