Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
821938089 committed Aug 25, 2024
1 parent caa3a7c commit e06cc88
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
7 changes: 7 additions & 0 deletions app/src/main/java/io/legado/app/ui/main/MainViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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, "")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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?) {
Expand All @@ -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 {
Expand All @@ -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) {
Expand Down Expand Up @@ -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
}
}

Expand Down Expand Up @@ -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 ->
Expand Down

0 comments on commit e06cc88

Please sign in to comment.