Avoid unnecessary child layout in webtoon recycler
This commit is contained in:
@@ -2,6 +2,7 @@ package org.koitharu.kotatsu.reader.ui.pager.webtoon
|
||||
|
||||
import android.content.Context
|
||||
import android.util.AttributeSet
|
||||
import android.view.View
|
||||
import androidx.core.view.ViewCompat.TYPE_TOUCH
|
||||
import androidx.core.view.forEach
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
@@ -13,12 +14,16 @@ class WebtoonRecyclerView @JvmOverloads constructor(
|
||||
) : RecyclerView(context, attrs, defStyleAttr) {
|
||||
|
||||
private var onPageScrollListeners: MutableList<OnPageScrollListener>? = null
|
||||
private val detachedViews = ArrayList<View>()
|
||||
|
||||
override fun onMeasure(widthSpec: Int, heightSpec: Int) {
|
||||
super.onMeasure(widthSpec, heightSpec)
|
||||
forEach { child ->
|
||||
(child as WebtoonFrameLayout).target.requestLayout()
|
||||
}
|
||||
override fun onChildDetachedFromWindow(child: View) {
|
||||
super.onChildDetachedFromWindow(child)
|
||||
detachedViews.add(child)
|
||||
}
|
||||
|
||||
override fun onChildAttachedToWindow(child: View) {
|
||||
super.onChildAttachedToWindow(child)
|
||||
detachedViews.remove(child)
|
||||
}
|
||||
|
||||
override fun startNestedScroll(axes: Int) = startNestedScroll(axes, TYPE_TOUCH)
|
||||
@@ -106,6 +111,15 @@ class WebtoonRecyclerView @JvmOverloads constructor(
|
||||
listeners.forEach { it.dispatchScroll(this, dy, centerPosition) }
|
||||
}
|
||||
|
||||
fun relayoutChildren() {
|
||||
forEach { child ->
|
||||
(child as WebtoonFrameLayout).target.requestLayout()
|
||||
}
|
||||
detachedViews.forEach {child ->
|
||||
(child as WebtoonFrameLayout).target.requestLayout()
|
||||
}
|
||||
}
|
||||
|
||||
abstract class OnPageScrollListener {
|
||||
|
||||
private var lastPosition = NO_POSITION
|
||||
|
||||
@@ -23,7 +23,7 @@ class WebtoonScalingFrame @JvmOverloads constructor(
|
||||
defStyles: Int = 0,
|
||||
) : FrameLayout(context, attrs, defStyles), ScaleGestureDetector.OnScaleGestureListener {
|
||||
|
||||
private val targetChild by lazy(LazyThreadSafetyMode.NONE) { getChildAt(0) }
|
||||
private val targetChild by lazy(LazyThreadSafetyMode.NONE) { getChildAt(0) as WebtoonRecyclerView }
|
||||
|
||||
private val scaleDetector = ScaleGestureDetector(context, this)
|
||||
private val gestureDetector = GestureDetectorCompat(context, GestureListener())
|
||||
@@ -96,6 +96,7 @@ class WebtoonScalingFrame @JvmOverloads constructor(
|
||||
if (newHeight != targetChild.height) {
|
||||
targetChild.layoutParams.height = newHeight
|
||||
targetChild.requestLayout()
|
||||
targetChild.relayoutChildren()
|
||||
}
|
||||
|
||||
if (scale < 1) {
|
||||
|
||||
Reference in New Issue
Block a user