mirror of
https://github.com/gedoor/legado.git
synced 2025-08-10 00:52:30 +00:00
优化
This commit is contained in:
@@ -3,6 +3,7 @@ package io.legado.app.ui.main
|
||||
import android.app.Application
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import androidx.recyclerview.widget.RecyclerView.RecycledViewPool
|
||||
import io.legado.app.base.BaseViewModel
|
||||
import io.legado.app.constant.AppConst
|
||||
import io.legado.app.constant.AppLog
|
||||
@@ -52,6 +53,12 @@ class MainViewModel(application: Application) : BaseViewModel(application) {
|
||||
val onUpBooksLiveData = MutableLiveData<Int>()
|
||||
private var upTocJob: Job? = null
|
||||
private var cacheBookJob: Job? = null
|
||||
val booksListRecycledViewPool = RecycledViewPool().apply {
|
||||
setMaxRecycledViews(0, 30)
|
||||
}
|
||||
val booksGridRecycledViewPool = RecycledViewPool().apply {
|
||||
setMaxRecycledViews(0, 100)
|
||||
}
|
||||
|
||||
override fun onCleared() {
|
||||
super.onCleared()
|
||||
|
||||
@@ -28,7 +28,15 @@ import io.legado.app.ui.file.HandleFileContract
|
||||
import io.legado.app.ui.main.MainFragmentInterface
|
||||
import io.legado.app.ui.main.MainViewModel
|
||||
import io.legado.app.ui.widget.dialog.WaitDialog
|
||||
import io.legado.app.utils.*
|
||||
import io.legado.app.utils.checkByIndex
|
||||
import io.legado.app.utils.getCheckedIndex
|
||||
import io.legado.app.utils.isAbsUrl
|
||||
import io.legado.app.utils.postEvent
|
||||
import io.legado.app.utils.readText
|
||||
import io.legado.app.utils.sendToClip
|
||||
import io.legado.app.utils.showDialogFragment
|
||||
import io.legado.app.utils.startActivity
|
||||
import io.legado.app.utils.toastOnUi
|
||||
|
||||
abstract class BaseBookshelfFragment(layoutId: Int) : VMBaseFragment<BookshelfViewModel>(layoutId),
|
||||
MainFragmentInterface {
|
||||
@@ -198,6 +206,11 @@ abstract class BaseBookshelfFragment(layoutId: Int) : VMBaseFragment<BookshelfVi
|
||||
}
|
||||
if (bookshelfLayout != rgLayout.getCheckedIndex()) {
|
||||
AppConfig.bookshelfLayout = rgLayout.getCheckedIndex()
|
||||
if (AppConfig.bookshelfLayout == 0) {
|
||||
activityViewModel.booksGridRecycledViewPool.clear()
|
||||
} else {
|
||||
activityViewModel.booksListRecycledViewPool.clear()
|
||||
}
|
||||
postEvent(EventBus.RECREATE, "")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package io.legado.app.ui.main.bookshelf.style1.books
|
||||
import android.annotation.SuppressLint
|
||||
import android.os.Bundle
|
||||
import android.view.View
|
||||
import android.view.ViewConfiguration
|
||||
import androidx.core.view.isGone
|
||||
import androidx.fragment.app.activityViewModels
|
||||
import androidx.lifecycle.Lifecycle
|
||||
@@ -79,7 +80,6 @@ class BooksFragment() : BaseFragment(R.layout.fragment_books),
|
||||
var bookSort = 0
|
||||
private set
|
||||
private var upLastUpdateTimeJob: Job? = null
|
||||
private var defaultScrollBarSize = 0
|
||||
private var enableRefresh = true
|
||||
|
||||
override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) {
|
||||
@@ -97,7 +97,6 @@ class BooksFragment() : BaseFragment(R.layout.fragment_books),
|
||||
|
||||
private fun initRecyclerView() {
|
||||
binding.rvBookshelf.setEdgeEffectColor(primaryColor)
|
||||
defaultScrollBarSize = binding.rvBookshelf.scrollBarSize
|
||||
upFastScrollerBar()
|
||||
binding.refreshLayout.setColorSchemeColors(accentColor)
|
||||
binding.refreshLayout.setOnRefreshListener {
|
||||
@@ -109,6 +108,11 @@ class BooksFragment() : BaseFragment(R.layout.fragment_books),
|
||||
} else {
|
||||
binding.rvBookshelf.layoutManager = GridLayoutManager(context, bookshelfLayout + 2)
|
||||
}
|
||||
if (bookshelfLayout == 0) {
|
||||
binding.rvBookshelf.setRecycledViewPool(activityViewModel.booksListRecycledViewPool)
|
||||
} else {
|
||||
binding.rvBookshelf.setRecycledViewPool(activityViewModel.booksGridRecycledViewPool)
|
||||
}
|
||||
binding.rvBookshelf.adapter = booksAdapter
|
||||
booksAdapter.registerAdapterDataObserver(object : RecyclerView.AdapterDataObserver() {
|
||||
override fun onItemRangeInserted(positionStart: Int, itemCount: Int) {
|
||||
@@ -136,7 +140,8 @@ class BooksFragment() : BaseFragment(R.layout.fragment_books),
|
||||
if (showBookshelfFastScroller) {
|
||||
binding.rvBookshelf.scrollBarSize = 0
|
||||
} else {
|
||||
binding.rvBookshelf.scrollBarSize = defaultScrollBarSize
|
||||
binding.rvBookshelf.scrollBarSize =
|
||||
ViewConfiguration.get(requireContext()).scaledScrollBarSize
|
||||
}
|
||||
}
|
||||
|
||||
@@ -258,6 +263,15 @@ class BooksFragment() : BaseFragment(R.layout.fragment_books),
|
||||
}
|
||||
}
|
||||
|
||||
override fun onDestroyView() {
|
||||
super.onDestroyView()
|
||||
/**
|
||||
* 将 RecyclerView 中的视图全部回收到 RecycledViewPool 中
|
||||
*/
|
||||
binding.rvBookshelf.setItemViewCacheSize(0)
|
||||
binding.rvBookshelf.adapter = null
|
||||
}
|
||||
|
||||
override fun open(book: Book) {
|
||||
when {
|
||||
book.isAudio ->
|
||||
|
||||
Reference in New Issue
Block a user