From 94fd2d47ec28a12cdf85c779462d83a6fda5c203 Mon Sep 17 00:00:00 2001 From: kunfei Date: Thu, 12 Mar 2020 15:34:02 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/ui/main/bookshelf/BookshelfAdapter.kt | 36 ------------ .../ui/main/bookshelf/BookshelfFragment.kt | 58 +++++++++++++------ .../main/res/layout/fragment_bookshelf.xml | 2 +- 3 files changed, 40 insertions(+), 56 deletions(-) delete mode 100644 app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfAdapter.kt diff --git a/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfAdapter.kt b/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfAdapter.kt deleted file mode 100644 index 55115ad94..000000000 --- a/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfAdapter.kt +++ /dev/null @@ -1,36 +0,0 @@ -package io.legado.app.ui.main.bookshelf - -import androidx.fragment.app.Fragment -import androidx.viewpager2.adapter.FragmentStateAdapter -import io.legado.app.data.entities.BookGroup -import io.legado.app.ui.main.bookshelf.books.BooksFragment - - -class BookshelfAdapter(fragment: Fragment, val callBack: CallBack) : - FragmentStateAdapter(fragment) { - - private val ids = hashSetOf() - - override fun getItemCount(): Int { - return callBack.groupSize - } - - override fun getItemId(position: Int): Long { - return callBack.getGroup(position).groupId.toLong() - } - - override fun containsItem(itemId: Long): Boolean { - return ids.contains(itemId) - } - - override fun createFragment(position: Int): Fragment { - val groupId = callBack.getGroup(position).groupId - ids.add(groupId.toLong()) - return BooksFragment.newInstance(position, groupId) - } - - interface CallBack { - val groupSize: Int - fun getGroup(position: Int): BookGroup - } -} \ 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 3608a79ed..55dca8d77 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 @@ -7,10 +7,12 @@ import android.view.Menu import android.view.MenuItem import android.view.View import androidx.appcompat.widget.SearchView +import androidx.fragment.app.Fragment +import androidx.fragment.app.FragmentManager +import androidx.fragment.app.FragmentStatePagerAdapter import androidx.lifecycle.LiveData import androidx.lifecycle.Observer import com.google.android.material.tabs.TabLayout -import com.google.android.material.tabs.TabLayoutMediator import io.legado.app.App import io.legado.app.R import io.legado.app.base.VMBaseFragment @@ -29,6 +31,7 @@ import io.legado.app.ui.book.download.DownloadActivity import io.legado.app.ui.book.group.GroupManageDialog import io.legado.app.ui.book.local.ImportBookActivity import io.legado.app.ui.book.search.SearchActivity +import io.legado.app.ui.main.bookshelf.books.BooksFragment import io.legado.app.ui.widget.text.AutoCompleteTextView import io.legado.app.utils.* import kotlinx.android.synthetic.main.dialog_bookshelf_config.view.* @@ -42,15 +45,14 @@ import org.jetbrains.anko.startActivity class BookshelfFragment : VMBaseFragment(R.layout.fragment_bookshelf), TabLayout.OnTabSelectedListener, SearchView.OnQueryTextListener, - GroupManageDialog.CallBack, - BookshelfAdapter.CallBack { + GroupManageDialog.CallBack { override val viewModel: BookshelfViewModel get() = getViewModel(BookshelfViewModel::class.java) - private lateinit var bookshelfAdapter: BookshelfAdapter private var bookGroupLiveData: LiveData>? = null private val bookGroups = mutableListOf() + private val fragmentMap = hashMapOf() override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) { setSupportToolbar(toolbar) @@ -79,26 +81,17 @@ class BookshelfFragment : VMBaseFragment(R.layout.fragment_b } } - override val groupSize: Int - get() = bookGroups.size - - override fun getGroup(position: Int): BookGroup { - return bookGroups[position] - } - private val selectedGroup: BookGroup get() = bookGroups[view_pager_bookshelf.currentItem] private fun initView() { + ATH.applyEdgeEffectColor(view_pager_bookshelf) tab_layout.isTabIndicatorFullWidth = false tab_layout.tabMode = TabLayout.MODE_SCROLLABLE tab_layout.setSelectedTabIndicatorColor(requireContext().accentColor) - ATH.applyEdgeEffectColor(view_pager_bookshelf) - bookshelfAdapter = BookshelfAdapter(this, this) - view_pager_bookshelf.adapter = bookshelfAdapter - TabLayoutMediator(tab_layout, view_pager_bookshelf) { tab, position -> - tab.text = bookGroups[position].groupName - }.attach() + tab_layout.setupWithViewPager(view_pager_bookshelf) + view_pager_bookshelf.offscreenPageLimit = 1 + view_pager_bookshelf.adapter = TabFragmentPageAdapter(childFragmentManager) } private fun initBookGroupData() { @@ -119,7 +112,7 @@ class BookshelfFragment : VMBaseFragment(R.layout.fragment_b bookGroups.add(AppConst.bookGroupAudio) } bookGroups.addAll(it) - bookshelfAdapter.notifyDataSetChanged() + view_pager_bookshelf.adapter?.notifyDataSetChanged() tab_layout.getTabAt(getPrefInt(PreferKey.saveTabPosition, 0))?.select() tab_layout.addOnTabSelectedListener(this) } @@ -149,7 +142,7 @@ class BookshelfFragment : VMBaseFragment(R.layout.fragment_b if (AppConfig.bookGroupAllShow) { bookGroups.add(0, AppConst.bookGroupAll) } - bookshelfAdapter.notifyDataSetChanged() + view_pager_bookshelf.adapter?.notifyDataSetChanged() } } @@ -212,4 +205,31 @@ class BookshelfFragment : VMBaseFragment(R.layout.fragment_b putPrefInt(PreferKey.saveTabPosition, it) } } + + private inner class TabFragmentPageAdapter internal constructor(fm: FragmentManager) : + FragmentStatePagerAdapter(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) { + + override fun getPageTitle(position: Int): CharSequence? { + return bookGroups[position].groupName + } + + override fun getItemPosition(`object`: Any): Int { + return POSITION_NONE + } + + override fun getItem(position: Int): Fragment { + val group = bookGroups[position] + var fragment = fragmentMap[group.groupId] + if (fragment == null) { + fragment = BooksFragment.newInstance(position, group.groupId) + fragmentMap[group.groupId] = fragment + } + return fragment + } + + override fun getCount(): Int { + return bookGroups.size + } + + } } \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_bookshelf.xml b/app/src/main/res/layout/fragment_bookshelf.xml index 2eb390c9a..ea0a9ab40 100644 --- a/app/src/main/res/layout/fragment_bookshelf.xml +++ b/app/src/main/res/layout/fragment_bookshelf.xml @@ -14,7 +14,7 @@ app:contentLayout="@layout/view_tab_layout_min" app:layout_constraintTop_toTopOf="parent" /> -