我的onTouchListener
的LinearLayout
有一个ListView
,并且我试图使用ACTION_DOWN
和ACTION_UP
数据来检测用户何时滑至下一个ListView
。但是,尽管MotionEvent
可以完美运行,但ACTION_DOWN
永远不等于ACTION_UP
。经过大量的搜索之后,我唯一能找到的解决方案是在调用事件时返回true,但是我已经这样做了。这是我的onTouchListener
代码
View.OnTouchListener mTouchListener = new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
downX = event.getX();
return true;
} else if (event.getAction() == MotionEvent.ACTION_UP) {
upX = event.getX();
if(userSwipedFarEnough)
doStuff()
return true;
}
return false;
}
};
我知道发生了什么,我的列表视图的滚动视图以某种方式窃取了action_down,因此没有被调用。当我的列表视图为空并且滚动正常时,我才意识到这一点。
根据触摸类型ACTION_DOWN ACTION_UP ACTION_MOVE,多次调用onTouch,所有这些可能一起发生。我想说出else if并仅使用if,它会捕获两个动作
我的解决方案是扩展ScrollView:
interface MyScrollViewActionDownListener{
fun onActionDown()
}
class MyScrollView: ScrollView
{
private var mActionDownListener: MyScrollViewActionDownListener? = null
constructor(context: Context, attributeSet: AttributeSet, defStyleAttr: Int): super(context, attributeSet, defStyleAttr)
constructor(context: Context):super(context)
constructor(context: Context, attributeSet: AttributeSet):super(context,attributeSet)
override fun onInterceptTouchEvent(ev: MotionEvent?): Boolean {
if(ev!!.action == MotionEvent.ACTION_DOWN){
mActionDownListener?.onActionDown()
}
return super.onInterceptTouchEvent(ev)
}
fun setActionDownListener(listener: MyScrollViewActionDownListener){
this.mActionDownListener = listener
}
}