Match extra layout space with scroll distance (#1076)

And increase recycler item view cache size.
This commit is contained in:
FooIbar 2024-08-04 23:45:11 +08:00 committed by GitHub
parent de8ef6dad7
commit a3dfd2efe6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 11 additions and 12 deletions

View file

@ -13,12 +13,7 @@ import androidx.recyclerview.widget.RecyclerView.NO_POSITION
* This layout manager uses the same package name as the support library in order to use a package * This layout manager uses the same package name as the support library in order to use a package
* protected method. * protected method.
*/ */
class WebtoonLayoutManager(context: Context) : LinearLayoutManager(context) { class WebtoonLayoutManager(context: Context, private val extraLayoutSpace: Int) : LinearLayoutManager(context) {
/**
* Extra layout space is set to half the screen height.
*/
private val extraLayoutSpace = context.resources.displayMetrics.heightPixels / 2
init { init {
isItemPrefetchEnabled = false isItemPrefetchEnabled = false
@ -27,6 +22,7 @@ class WebtoonLayoutManager(context: Context) : LinearLayoutManager(context) {
/** /**
* Returns the custom extra layout space. * Returns the custom extra layout space.
*/ */
@Deprecated("Deprecated in Java")
override fun getExtraLayoutSpace(state: RecyclerView.State): Int { override fun getExtraLayoutSpace(state: RecyclerView.State): Int {
return extraLayoutSpace return extraLayoutSpace
} }

View file

@ -47,10 +47,15 @@ class WebtoonViewer(val activity: ReaderActivity, val isContinuous: Boolean = tr
*/ */
private val frame = WebtoonFrame(activity) private val frame = WebtoonFrame(activity)
/**
* Distance to scroll when the user taps on one side of the recycler view.
*/
private val scrollDistance = activity.resources.displayMetrics.heightPixels * 3 / 4
/** /**
* Layout manager of the recycler view. * Layout manager of the recycler view.
*/ */
private val layoutManager = WebtoonLayoutManager(activity) private val layoutManager = WebtoonLayoutManager(activity, scrollDistance)
/** /**
* Configuration used by this viewer, like allow taps, or crop image borders. * Configuration used by this viewer, like allow taps, or crop image borders.
@ -62,11 +67,6 @@ class WebtoonViewer(val activity: ReaderActivity, val isContinuous: Boolean = tr
*/ */
private val adapter = WebtoonAdapter(this) private val adapter = WebtoonAdapter(this)
/**
* Distance to scroll when the user taps on one side of the recycler view.
*/
private var scrollDistance = activity.resources.displayMetrics.heightPixels * 3 / 4
/** /**
* Currently active item. It can be a chapter page or a chapter transition. * Currently active item. It can be a chapter page or a chapter transition.
*/ */
@ -79,6 +79,7 @@ class WebtoonViewer(val activity: ReaderActivity, val isContinuous: Boolean = tr
.threshold .threshold
init { init {
recycler.setItemViewCacheSize(RecyclerViewCacheSize)
recycler.isVisible = false // Don't let the recycler layout yet recycler.isVisible = false // Don't let the recycler layout yet
recycler.layoutParams = ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT) recycler.layoutParams = ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT)
recycler.isFocusable = false recycler.isFocusable = false
@ -359,3 +360,5 @@ class WebtoonViewer(val activity: ReaderActivity, val isContinuous: Boolean = tr
) )
} }
} }
private const val RecyclerViewCacheSize = 4