我有一个带有 leftPadding = 48dp
、topPadding = 24dp
和 clipToPadding = false
的水平 RecyclerView
。它从左侧的空白区域开始,但是当用户滚动列表时,它的项目被绘制在那个(以前是空白的)空间上。顶部空间始终为空。
这个 RecyclerView
在 FrameLayout 中,foreground = selectableItemBackground
。
我的问题来自 RecyclerView
消耗并忽略左侧和顶部空间的触摸,这意味着 OnClickListener
不会被触发,无论是附加到FrameLayout
或 RecyclerView
。
我已经在 RecyclerView
上尝试了 clickable = false
和 focusable = false
,但它不起作用。
我在寻找什么:
编辑:我创建了一个简单的项目来显示我正在谈论的问题:https://github.com/dadino/recyclerviewemptyspacestest 有 2 次提交,第一次我 try catch 对父 View 的点击,第二次我 try catch 对 RecyclerView 本身的点击。它们都不起作用。
最佳答案
您必须创建您的自定义 RecyclerView
实现,您可以在其中监听触摸事件并基于此执行过滤。
class MyRecyclerView @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : RecyclerView(context, attrs, defStyleAttr) {
private var isValid = false
private var x: Int = 0
private var y: Int = 0
// this will help us to understand whether the event can be considered a touch or scroll
private val delta: Int = ViewConfiguration.get(getContext()).scaledTouchSlop
override fun onTouchEvent(e: MotionEvent?): Boolean {
val onTouchEvent = super.onTouchEvent(e)
when (e?.action) {
MotionEvent.ACTION_DOWN -> {
// saving initial touch location
x = e.rawX.toInt()
y = e.rawY.toInt()
isValid = true
}
MotionEvent.ACTION_MOVE -> {
if (Math.abs(e.rawX - x) > delta ||
Math.abs(e.rawY - y) > delta) {
// if a scroll happens - no longer consider this movement as valid
// this is needed for handling scroll on the inner part of `RecyclerView`
isValid = false
}
}
MotionEvent.ACTION_UP -> {
if (isValid && Math.abs(e.rawX - x) < delta &&
Math.abs(e.rawY - y) < delta &&
isInRightArea(e)) {
// 1. if the movement is still valid
// 2. we have actually performed a click
// 3. the click is in expected rectangle
// then perform click
performClick()
}
}
}
return onTouchEvent
}
// This is needed in order to handle the edge case, when a click listener
// would be fired when performing a click between the items of `RecyclerView`
private fun isInRightArea(e: MotionEvent): Boolean {
val r = Rect()
getGlobalVisibleRect(r)
r.left = paddingLeft
r.top = r.top + paddingTop
return !r.contains(e.rawX.toInt(), e.rawY.toInt())
}
}
结果:
关于android - 回收站查看 : listen to padding click events,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46930625/