feat: 优化代码

pull/154/head
kunfei 5 years ago
parent ad918ae671
commit 94fd2d47ec
  1. 36
      app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfAdapter.kt
  2. 58
      app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfFragment.kt
  3. 2
      app/src/main/res/layout/fragment_bookshelf.xml

@ -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<Long>()
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
}
}

@ -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<BookshelfViewModel>(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<List<BookGroup>>? = null
private val bookGroups = mutableListOf<BookGroup>()
private val fragmentMap = hashMapOf<Int, Fragment>()
override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) {
setSupportToolbar(toolbar)
@ -79,26 +81,17 @@ class BookshelfFragment : VMBaseFragment<BookshelfViewModel>(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<BookshelfViewModel>(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<BookshelfViewModel>(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<BookshelfViewModel>(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
}
}
}

@ -14,7 +14,7 @@
app:contentLayout="@layout/view_tab_layout_min"
app:layout_constraintTop_toTopOf="parent" />
<androidx.viewpager2.widget.ViewPager2
<androidx.viewpager.widget.ViewPager
android:id="@+id/view_pager_bookshelf"
android:layout_width="match_parent"
android:layout_height="0dp"

Loading…
Cancel
Save