android - 在可滑动列表视图中具有 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?
解决方案
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
推荐阅读
- remote-access - 如何在 AnyDesk 上最大化屏幕。RDP 与 AnyDesk
- python - 从 Flask 后端向 Flutter 前端发送和渲染多个图像
- .net-6.0 - 如何激活 C# 10 功能 Visual Studio 2022
- docker - Docker 运行:uwsgi 授予权限被拒绝
- c# - OpenXML 验证错误说 knownFonts 属性未声明,但它是
- javascript - 在保留 HTML 格式的同时拆分字符串
- webpack - 如何在 2021 年为 UMD 添加 CSS 到 Webpack 5
- python - 使用 Waitress 会为 django 项目产生“此页面不工作”,但适用于 python manage.py runserver
- flutter - 我在 App Store 上有一个使用 SwiftUI 制作的实时 iOS 应用程序,并且我用 Flutter 制作了相同的应用程序。如何更换 App Store 上的应用程序?
- java - 选择目录以在 chrome 中下载带有 selenium 的文件