gpt4 book ai didi

java - 在双向 Recyclerview 中处理水平滚动时禁用 SwipeRefreshLayout

转载 作者:行者123 更新时间:2023-12-01 18:30:46 36 4
gpt4 key购买 nike

我有一个带有自定义 LayoutManager 的 RecyclerView,它具有双向滚动逻辑。所以,我重写了它的方法:

override fun canScrollVertically(): Boolean = true
override fun canScrollHorizontally(): Boolean = true

然后我将 RecyclerView 放入自定义 SwipeRefreshLayout 中,以防止用户水平滚动列表时的“滑动刷新”操作:

class CustomSwipeToRefresh
@JvmOverloads
constructor(
context: Context,
attrs: AttributeSet? = null
) : SwipeRefreshLayout(context, attrs) {

private val touchSlop: Int = ViewConfiguration.get(context).scaledTouchSlop
private var startX = 0f
private var forbidSwipe = false

override fun onInterceptTouchEvent(event: MotionEvent): Boolean {
when (event.action) {
ACTION_DOWN -> startX = event.x
ACTION_MOVE -> {
if (abs(event.x - startX) > touchSlop || forbidSwipe) {
forbidSwipe = true
return false
}
}
ACTION_CANCEL, ACTION_UP -> {
forbidSwipe = false
}
}
return super.onInterceptTouchEvent(event)
}

}

但是这个解决方案不起作用。尝试了这个解决方案(手动禁用),但它也不起作用:

override fun onInterceptTouchEvent(event: MotionEvent): Boolean {
when (event.action) {
ACTION_DOWN -> startX = event.x
ACTION_MOVE -> {
if (abs(event.x - startX) > touchSlop || forbidSwipe) {
forbidSwipe = true
isEnabled = false
return false
}
}
ACTION_CANCEL, ACTION_UP -> {
isEnabled = true
forbidSwipe = false
}
}
return super.onInterceptTouchEvent(event)
}

无论如何,如果我写

refreshLayout.isEnabled = false

在我的 Fragment 中的 onViewCreated 中,它被禁用了。

还尝试在RecyclerView.OnScrollListener中禁用SwipeRefreshLayout,但没有帮助。我的代码或 SwipeRefreshLayout 有什么问题?

最佳答案

好的。经过几天的谷歌搜索,我发现显示和移动微调器的所有魔力都位于 NestedScrollingParent 方法中。因此,我重写了 onNestedScroll 方法,如果 forbidSwipe == true 我只是忽略 super 方法:

class CustomSwipeToRefresh
@JvmOverloads
constructor(
context: Context,
attrs: AttributeSet? = null
) : SwipeRefreshLayout(context, attrs) {

private val touchSlop: Int = ViewConfiguration.get(context).scaledTouchSlop
private var startX = 0f
private var startY = 0f
private var forbidSwipe = false
private var isStartScrolledByY = false

override fun onInterceptTouchEvent(event: MotionEvent): Boolean {
when (event.action) {
ACTION_DOWN -> {
startX = event.x
startY = event.y
}
ACTION_MOVE -> {
val isScrolledByX = abs(event.x - startX) > touchSlop
val isScrolledByY = abs(event.y - startY) > touchSlop
if (!forbidSwipe && isScrolledByY) {
isStartScrolledByY = true
}
if ((isScrolledByX || forbidSwipe) && !isStartScrolledByY) {
forbidSwipe = true
return false
}
}
ACTION_CANCEL, ACTION_UP -> {
forbidSwipe = false
isStartScrolledByY = false
}
}
return super.onInterceptTouchEvent(event)
}

override fun onNestedScroll(target: View, dxConsumed: Int, dyConsumed: Int, dxUnconsumed: Int, dyUnconsumed: Int) {
if (forbidSwipe) return
super.onNestedScroll(target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed)
}

}

关于java - 在双向 Recyclerview 中处理水平滚动时禁用 SwipeRefreshLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60169798/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com