From 3d530ca191d833fe5540a50bdb3b7269a0e89950 Mon Sep 17 00:00:00 2001 From: gedoor Date: Sat, 17 Oct 2020 22:03:30 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/legado/app/ui/main/MainActivity.kt | 53 ++++--------------- .../ui/main/bookshelf/BookshelfFragment.kt | 4 +- 2 files changed, 12 insertions(+), 45 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 26879ae87..db221bede 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 @@ -178,48 +178,23 @@ class MainActivity : VMBaseActivity(R.layout.activity_main), private inner class TabFragmentPageAdapter(fm: FragmentManager) : FragmentStatePagerAdapter(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) { - val bookshelfFragment: Fragment - get() { - if (!fragmentMap.containsKey(0)) { - fragmentMap[0] = BookshelfFragment() - } - return fragmentMap.getValue(0) - } - - val exploreFragment: Fragment - get() { - if (!fragmentMap.containsKey(1)) { - fragmentMap[1] = ExploreFragment() - } - return fragmentMap.getValue(1) - } - - val rssFragment: Fragment - get() { - if (!fragmentMap.containsKey(2)) { - fragmentMap[2] = RssFragment() - } - return fragmentMap.getValue(2) - } - - val myFragment: Fragment - get() { - if (!fragmentMap.containsKey(3)) { - fragmentMap[3] = MyFragment() - } - return fragmentMap.getValue(3) + private fun getId(position: Int): Int { + return when (position) { + 2 -> if (AppConfig.isShowRSS) 2 else 3 + else -> position } + } override fun getItemPosition(`object`: Any): Int { return POSITION_NONE } override fun getItem(position: Int): Fragment { - return when (position) { - 0 -> bookshelfFragment - 1 -> exploreFragment - 2 -> if (AppConfig.isShowRSS) rssFragment else myFragment - else -> myFragment + return when (getId(position)) { + 0 -> BookshelfFragment() + 1 -> ExploreFragment() + 2 -> RssFragment() + else -> MyFragment() } } @@ -229,13 +204,7 @@ class MainActivity : VMBaseActivity(R.layout.activity_main), 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 - } + fragmentMap[getId(position)] = fragment return fragment } 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 cd967ad33..1a0dfec74 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 @@ -232,9 +232,7 @@ class BookshelfFragment : VMBaseFragment(R.layout.fragment_b 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 - } + fragmentMap[group.groupId] = fragment return fragment }