ViewPager пуст при возврате к нему с помощью кнопки назад - PullRequest
0 голосов
/ 12 января 2019

Мое приложение довольно простое. O имеют 1 активность и три фрагмента. При загрузке действия загружается фрагмент, состоящий из ViewPager. Я использую ViewPager для показа новостей, первый фрагмент в ViewPager показывает новые новости, второй показывает прочитанные новости. Все работает хорошо, но когда я нажимаю на новости и открываю ContentFragment, чтобы показать его, а затем нажимаю кнопку назад, чтобы вернуться к фрагменту с ViewPager. ContentFragment исчезнет, ​​но фрагмент с ViewPager останется пустым. Я отладил и убедился, что такие методы, как onCreateView и т. Д. Вызываются, но фрагмент все еще пуст, я запутался. Что с этим не так? Спасибо

MainActivity

<androidx.drawerlayout.widget.DrawerLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".activity.MainActivity"
        android:id="@+id/activity_main_layout_id">


    <androidx.coordinatorlayout.widget.CoordinatorLayout android:layout_width="match_parent"
                                                         android:layout_height="match_parent">

        <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/main_content"
        >

        </RelativeLayout>


    </androidx.coordinatorlayout.widget.CoordinatorLayout>


    <com.google.android.material.navigation.NavigationView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:fitsSystemWindows="true"
            android:id="@+id/navigation_view"
    >

        <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/swipe_refresh_layout"
        >

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


            </androidx.recyclerview.widget.RecyclerView>
        </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

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


</androidx.drawerlayout.widget.DrawerLayout>

При запуске показывает фрагмент с ViewPager

private fun showList() {
    val fragmentTransaction = supportFragmentManager.beginTransaction()
    val newsPagerFragment = NewsPagerFragment()
    fragmentTransaction.replace(R.id.main_content, newsPagerFragment)
    fragmentTransaction.commit()
}

NewsPagerFragment

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              xmlns:ads="http://schemas.android.com/apk/res-auto"
              android:layout_width="match_parent" android:layout_height="match_parent"
              tools:context=".fragment.NewsPagerFragment"
              android:orientation="vertical"
              android:id="@+id/news_list_fragment">

    <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/swipe_refresh_layout"
    >
        <androidx.viewpager.widget.ViewPager
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/view_pager"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

        </androidx.viewpager.widget.ViewPager>

    </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

    <com.google.android.gms.ads.AdView
            android:id="@+id/adView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            ads:adUnitId="@string/ads_banner_id_test"
            ads:adSize="BANNER"/>

</LinearLayout>

NewsListDataFragment, используемый в ViewPager для отображения новостей.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              xmlns:app="http://schemas.android.com/apk/res-auto"
              android:layout_width="match_parent" android:layout_height="match_parent"
              tools:context=".fragment.NewsPagerFragment"
              android:orientation="vertical"
              android:id="@+id/news_list_fragment">

    <androidx.recyclerview.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/news_list_recycler"
            app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager">

    </androidx.recyclerview.widget.RecyclerView>

</LinearLayout>

И когда мы нажимаем на новости, они звонят

private fun onPreviewNewsClick(news: PostEntity) {       
        val contentFragment = NewsContentFragment()        
        val transaction = fragmentManager.beginTransaction()      
        transaction.replace(R.id.main_content, contentFragment)
        transaction.commit()
    }

NewsContentFragment

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             xmlns:tools="http://schemas.android.com/tools"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             tools:context=".fragment.NewsContentFragment"
             android:padding="5dp"
>

   <WebView
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:id="@+id/news_content_webview"
   >
   </WebView>

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