还原自滚动专题优化

This commit is contained in:
juntao
2021-02-04 09:37:56 +08:00
parent 0a455c7b09
commit dcbc2fe352
2 changed files with 118 additions and 15 deletions

View File

@ -1,6 +1,7 @@
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
@ -10,8 +11,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 {
@ -20,7 +21,6 @@ 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,22 +59,16 @@ class AutoScrollRecyclerViewContainerView(context: Context, @Nullable attrs: Att
fun resumeScrolling() {
if (mIsScrolling) return
if (mIsScrolling) pauseScrolling()
mIsScrollable = true
mIsScrolling = true
AppExecutor.lightWeightIoExecutor.execute {
while (mIsScrolling && autoScrollTask != null) {
if (System.currentTimeMillis() - mLastScrollTime >= AUTO_SCROLL_INTERVAL) {
mLastScrollTime = System.currentTimeMillis()
AppExecutor.uiExecutor.execute(autoScrollTask!!)
}
}
}
postDelayed(autoScrollTask, AUTO_SCROLL_INTERVAL)
}
@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
fun pauseScrolling() {
mIsScrolling = false
removeCallbacks(autoScrollTask)
}
fun setLifeCycleOwner(lifeCycleOwner: Lifecycle) {
@ -82,12 +76,12 @@ class AutoScrollRecyclerViewContainerView(context: Context, @Nullable attrs: Att
}
companion object {
private const val AUTO_SCROLL_INTERVAL: Long = 16
private const val SCROLL_SLOP = 1
private const val AUTO_SCROLL_INTERVAL: Long = 10
}
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)
@ -98,10 +92,11 @@ 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(SCROLL_SLOP, SCROLL_SLOP)
child.scrollBy(mScrollSlop, mScrollSlop)
}
}
containerView.postDelayed(containerView.autoScrollTask, AUTO_SCROLL_INTERVAL)
}
}
}
}
}

View File

@ -0,0 +1,108 @@
//package com.gh.common.view
//
//import android.content.Context
//import android.util.AttributeSet
//import android.view.MotionEvent
//import android.widget.LinearLayout
//import androidx.annotation.Nullable
//import androidx.core.view.children
//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
//
// TODO 多页面会出现问题
//class AutoScrollRecyclerViewContainerView(context: Context, @Nullable attrs: AttributeSet?)
// : LinearLayout(context, attrs), LifecycleObserver {
//
// var autoScrollTask: AutoScrollTask?
//
// private var mIsScrolling = false
// private var mIsScrollable = false
// private var mLastScrollTime = 0L
//
// private var mOnItemTouchListener: RecyclerView.OnItemTouchListener? = null
//
// init {
// autoScrollTask = AutoScrollTask(this)
// }
//
// override fun onAttachedToWindow() {
// super.onAttachedToWindow()
//
// if (mOnItemTouchListener == null) {
// mOnItemTouchListener = object : RecyclerView.OnItemTouchListener {
// override fun onTouchEvent(rv: RecyclerView, e: MotionEvent) {}
// override fun onInterceptTouchEvent(rv: RecyclerView, e: MotionEvent) = rv.scrollState == RecyclerView.SCROLL_STATE_DRAGGING
// override fun onRequestDisallowInterceptTouchEvent(disallowIntercept: Boolean) {}
// }
// }
//
// for (child in children) {
// if (child is RecyclerView) {
// child.removeOnItemTouchListener(mOnItemTouchListener!!)
// child.addOnItemTouchListener(mOnItemTouchListener!!)
// }
// }
//
// resumeScrolling()
// }
//
// override fun onDetachedFromWindow() {
// super.onDetachedFromWindow()
//
// pauseScrolling()
// }
//
// @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
// fun resumeScrolling() {
// if (mIsScrolling) return
//
// mIsScrollable = true
// mIsScrolling = true
//
// 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
// }
//
// fun setLifeCycleOwner(lifeCycleOwner: Lifecycle) {
// lifeCycleOwner.addObserver(this)
// }
//
// companion object {
// private const val AUTO_SCROLL_INTERVAL: Long = 16
// private const val SCROLL_SLOP = 1
// }
//
// class AutoScrollTask(reference: AutoScrollRecyclerViewContainerView?) : Runnable {
// private val mReference: WeakReference<AutoScrollRecyclerViewContainerView?>?
//
// init {
// mReference = WeakReference(reference)
// }
//
// override fun run() {
// val containerView: AutoScrollRecyclerViewContainerView? = mReference?.get()
// if (containerView != null && containerView.mIsScrolling && containerView.mIsScrollable) {
// for (child in containerView.children) {
// if (child is RecyclerView) {
// child.scrollBy(SCROLL_SLOP, SCROLL_SLOP)
// }
// }
// }
// }
// }
//}