首页 > 解决方案 > 在可滑动列表视图中具有 onClickListener 的视图不能再滑动,而仅响应 onClick 事件

问题描述

我在这个 url 之后实现了一个可滑动的列表视图:https://jayrambhia.com/blog/swipe-listview,唯一的区别是我只有 MotionEvent.ACTION_DOWN 和 MotionEvent.ACTION_UP 并根据 deltaX 检测 MotionEvent.ACTION_UP 中的运动价值。列表项的自定义视图是这样的布局,包含在 Linearlayout 中:

文本视图1 | 文本视图2 文本视图3

在刷卡之前,它看起来像:

在此处输入图像描述

最初 TextView2 和 3 是隐藏的,当滑动该视图时,它们将显示如下:

在此处输入图像描述

我为所有 3 个 TextView 设置了 onClickListener,发现将 onClickListener 设置为 TextView1,如果从 TextView1 开始,我无法滑动,并且由于 TextView2 和 3 最初是隐藏的,因此仍然能够在 TextView1 之外滑动。似乎 TextView1 只响应 OnClickLisener。

我还尝试为 TextView1 调用这些:

setClickable(true);
setEnabled(true);
setFocusable(true);
setFocusableInTouchMode(true);

我错过了什么?任何提示将不胜感激。提前致谢!

编辑:

我注意到,如果我将手指放在 TextView1 和它的父 Linearlayout 之间,那么滑动就会起作用。所以看起来子视图(TextView1)由于子视图的onClickListener而禁用了它的父视图(Linearlayout)onTouchListener?从子视图开始滑动时如何获取 onTouchEvent?

标签: androidandroid-listviewtouch-eventswipe-gesture

解决方案


I post the answer specific to my situation. Please check this url to get code of implementing swipable listview: https://jayrambhia.com/blog/swipe-listview.

In my code, I attached the touch listener to the listview item's layout view (convertView):

convertView.setOnTouchListener(new SwipeDetector(viewHolder, position, data, mContext));

And I also attached the same touch listener to "TextView1" of the view holder for this convertView: (This makes the swiping on "TextView1" work.)

TextView1.setOnTouchListener(new SwipeDetector(this, position, data, act));

Where

data - a java class holding the information for each list item;
position - a index of the list item
act / mContext - the activity instance
viewHolder - View holder class defined in the adapter
this - the Viewholder

推荐阅读