From f1897ae596e800a112d1b2982b5e5bfa439c0436 Mon Sep 17 00:00:00 2001 From: gedoor Date: Thu, 10 Sep 2020 21:36:48 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dfragment=E5=A4=8D=E7=94=A8?= =?UTF-8?q?=E5=BC=95=E8=B5=B7=E7=9A=84=E5=8F=8C=E5=87=BB=E4=B9=A6=E6=9E=B6?= =?UTF-8?q?=E5=88=B0=E9=A1=B6=E7=AB=AF=E5=9C=A8=E5=88=87=E6=8D=A2=E4=B8=BB?= =?UTF-8?q?=E9=A2=98=E6=A8=A1=E5=BC=8F=E5=90=8E=E5=A4=B1=E6=95=88=E7=9A=84?= =?UTF-8?q?bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/legado/app/ui/main/MainActivity.kt | 60 +++++++++++++++---- .../ui/main/bookshelf/BookshelfFragment.kt | 14 +++-- 2 files changed, 58 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/main/MainActivity.kt b/app/src/main/java/io/legado/app/ui/main/MainActivity.kt index 934bb56f8..d1cd2dea4 100644 --- a/app/src/main/java/io/legado/app/ui/main/MainActivity.kt +++ b/app/src/main/java/io/legado/app/ui/main/MainActivity.kt @@ -3,6 +3,7 @@ package io.legado.app.ui.main import android.os.Bundle import android.view.KeyEvent import android.view.MenuItem +import android.view.ViewGroup import androidx.fragment.app.Fragment import androidx.fragment.app.FragmentManager import androidx.fragment.app.FragmentStatePagerAdapter @@ -28,6 +29,7 @@ import io.legado.app.utils.* import kotlinx.android.synthetic.main.activity_main.* import org.jetbrains.anko.toast + class MainActivity : VMBaseActivity(R.layout.activity_main), BottomNavigationView.OnNavigationItemSelectedListener, BottomNavigationView.OnNavigationItemReselectedListener, @@ -37,13 +39,7 @@ class MainActivity : VMBaseActivity(R.layout.activity_main), private var exitTime: Long = 0 private var bookshelfReselected: Long = 0 private var pagePosition = 0 - private val fragmentId = arrayOf(0, 1, 2, 3) - private val fragmentMap = mapOf( - Pair(fragmentId[0], BookshelfFragment()), - Pair(fragmentId[1], ExploreFragment()), - Pair(fragmentId[2], RssFragment()), - Pair(fragmentId[3], MyFragment()) - ) + private val fragmentMap = hashMapOf() override fun onActivityCreated(savedInstanceState: Bundle?) { ATH.applyEdgeEffectColor(view_pager_main) @@ -171,20 +167,48 @@ class MainActivity : VMBaseActivity(R.layout.activity_main), private inner class TabFragmentPageAdapter(fm: FragmentManager) : FragmentStatePagerAdapter(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) { + private fun getBookshelfFragment(): Fragment { + if (!fragmentMap.containsKey(0)) { + fragmentMap[0] = BookshelfFragment() + } + return fragmentMap.getValue(0) + } + + private fun getExploreFragment(): Fragment { + if (!fragmentMap.containsKey(1)) { + fragmentMap[1] = ExploreFragment() + } + return fragmentMap.getValue(1) + } + + private fun getRssFragment(): Fragment { + if (!fragmentMap.containsKey(2)) { + fragmentMap[2] = RssFragment() + } + return fragmentMap.getValue(2) + } + + private fun getMyFragment(): Fragment { + if (!fragmentMap.containsKey(3)) { + fragmentMap[3] = MyFragment() + } + return fragmentMap.getValue(3) + } + override fun getItemPosition(`object`: Any): Int { return POSITION_NONE } override fun getItem(position: Int): Fragment { return when (position) { - 0 -> fragmentMap.getValue(fragmentId[0]) - 1 -> fragmentMap.getValue(fragmentId[1]) + 0 -> getBookshelfFragment() + 1 -> getExploreFragment() 2 -> if (AppConfig.isShowRSS) { - fragmentMap.getValue(fragmentId[2]) + getRssFragment() } else { - fragmentMap.getValue(fragmentId[3]) + getMyFragment() } - else -> fragmentMap.getValue(fragmentId[3]) + else -> getMyFragment() } } @@ -192,6 +216,18 @@ class MainActivity : VMBaseActivity(R.layout.activity_main), return if (AppConfig.isShowRSS) 4 else 3 } + override fun instantiateItem(container: ViewGroup, position: Int): Any { + val fragment = super.instantiateItem(container, position) as Fragment + val id = when (position) { + 2 -> if (AppConfig.isShowRSS) 2 else 3 + else -> position + } + if (!fragmentMap.containsKey(id)) { + fragmentMap[id] = fragment + } + return fragment + } + } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfFragment.kt b/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfFragment.kt index 4444f33d7..28ae162d1 100644 --- a/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfFragment.kt +++ b/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfFragment.kt @@ -2,10 +2,7 @@ package io.legado.app.ui.main.bookshelf import android.annotation.SuppressLint import android.os.Bundle -import android.view.LayoutInflater -import android.view.Menu -import android.view.MenuItem -import android.view.View +import android.view.* import androidx.appcompat.widget.SearchView import androidx.fragment.app.Fragment import androidx.fragment.app.FragmentManager @@ -294,5 +291,14 @@ class BookshelfFragment : VMBaseFragment(R.layout.fragment_b return bookGroups.size } + override fun instantiateItem(container: ViewGroup, position: Int): Any { + val fragment = super.instantiateItem(container, position) as BooksFragment + val group = bookGroups[position] + if (!fragmentMap.containsKey(group.groupId)) { + fragmentMap[group.groupId] = fragment + } + return fragment + } + } } \ No newline at end of file