分享一段ViewPager2中RecyclerView滑动的问题

ViewPager2中插入RecyclerView,滑动过于敏感,下面记录一种方法,其实就是重新处理事件分发。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import androidx.recyclerview.widget.RecyclerView
import kotlin.math.abs

class RecyclerViewAtViewPager2 : RecyclerView {
constructor(context: Context) : super(context) {}
constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {}
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
context,
attrs,
defStyleAttr
) {
}

private var disallowIntercept = false

private var startX = 0
private var startY = 0
var isDispatch: Boolean = true
override fun dispatchTouchEvent(ev: MotionEvent): Boolean {
if (isDispatch) {
when (ev.action) {
MotionEvent.ACTION_DOWN -> {
startX = ev.x.toInt()
startY = ev.y.toInt()
parent.requestDisallowInterceptTouchEvent(true)
}
MotionEvent.ACTION_MOVE -> {
val endX = ev.x.toInt()
val endY = ev.y.toInt()
val disX = abs(endX - startX)
val disY = abs(endY - startY)
if (disX > disY) {
//为了解决RecyclerView嵌套RecyclerView时横向滑动的问题
if (disallowIntercept) {
parent.requestDisallowInterceptTouchEvent(disallowIntercept)
} else {
parent.requestDisallowInterceptTouchEvent(canScrollHorizontally(startX - endX))
}
} else {
parent.requestDisallowInterceptTouchEvent(true)
}
}
MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> parent.requestDisallowInterceptTouchEvent(
false
)
}
}

return super.dispatchTouchEvent(ev)
}

override fun requestDisallowInterceptTouchEvent(disallowIntercept: Boolean) {
this.disallowIntercept = disallowIntercept
super.requestDisallowInterceptTouchEvent(disallowIntercept)

}
}

坚持原创分享,您的支持将鼓励我继续创作!