提高首页图集的滚动流畅度
This commit is contained in:
@ -1,7 +1,6 @@
|
||||
package com.gh.common.view
|
||||
|
||||
import android.content.Context
|
||||
import android.os.Build
|
||||
import android.util.AttributeSet
|
||||
import android.view.MotionEvent
|
||||
import android.widget.LinearLayout
|
||||
@ -11,8 +10,8 @@ import androidx.lifecycle.Lifecycle
|
||||
import androidx.lifecycle.LifecycleObserver
|
||||
import androidx.lifecycle.OnLifecycleEvent
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import com.gh.common.AppExecutor
|
||||
import java.lang.ref.WeakReference
|
||||
import java.util.*
|
||||
|
||||
class AutoScrollRecyclerViewContainerView(context: Context, @Nullable attrs: AttributeSet?)
|
||||
: LinearLayout(context, attrs), LifecycleObserver {
|
||||
@ -21,6 +20,7 @@ class AutoScrollRecyclerViewContainerView(context: Context, @Nullable attrs: Att
|
||||
|
||||
private var mIsScrolling = false
|
||||
private var mIsScrollable = false
|
||||
private var mLastScrollTime = 0L
|
||||
|
||||
private var mOnItemTouchListener: RecyclerView.OnItemTouchListener? = null
|
||||
|
||||
@ -59,16 +59,22 @@ class AutoScrollRecyclerViewContainerView(context: Context, @Nullable attrs: Att
|
||||
fun resumeScrolling() {
|
||||
if (mIsScrolling) return
|
||||
|
||||
if (mIsScrolling) pauseScrolling()
|
||||
mIsScrollable = true
|
||||
mIsScrolling = true
|
||||
postDelayed(autoScrollTask, AUTO_SCROLL_INTERVAL)
|
||||
|
||||
AppExecutor.lightWeightIoExecutor.execute {
|
||||
while (mIsScrolling && autoScrollTask != null) {
|
||||
if (System.currentTimeMillis() - mLastScrollTime >= AUTO_SCROLL_INTERVAL) {
|
||||
mLastScrollTime = System.currentTimeMillis()
|
||||
AppExecutor.uiExecutor.execute(autoScrollTask!!)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
|
||||
fun pauseScrolling() {
|
||||
mIsScrolling = false
|
||||
removeCallbacks(autoScrollTask)
|
||||
}
|
||||
|
||||
fun setLifeCycleOwner(lifeCycleOwner: Lifecycle) {
|
||||
@ -76,12 +82,12 @@ class AutoScrollRecyclerViewContainerView(context: Context, @Nullable attrs: Att
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val AUTO_SCROLL_INTERVAL: Long = 10
|
||||
private const val AUTO_SCROLL_INTERVAL: Long = 16
|
||||
private const val SCROLL_SLOP = 1
|
||||
}
|
||||
|
||||
class AutoScrollTask(reference: AutoScrollRecyclerViewContainerView?) : Runnable {
|
||||
private val mReference: WeakReference<AutoScrollRecyclerViewContainerView?>?
|
||||
private val mScrollSlop = if (Build.MODEL.toLowerCase(Locale.getDefault()) == "mumu") 1 else 2
|
||||
|
||||
init {
|
||||
mReference = WeakReference(reference)
|
||||
@ -92,10 +98,9 @@ class AutoScrollRecyclerViewContainerView(context: Context, @Nullable attrs: Att
|
||||
if (containerView != null && containerView.mIsScrolling && containerView.mIsScrollable) {
|
||||
for (child in containerView.children) {
|
||||
if (child is RecyclerView) {
|
||||
child.scrollBy(mScrollSlop, mScrollSlop)
|
||||
child.scrollBy(SCROLL_SLOP, SCROLL_SLOP)
|
||||
}
|
||||
}
|
||||
containerView.postDelayed(containerView.autoScrollTask, AUTO_SCROLL_INTERVAL)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user