Android StateListDrawable 在 MultiTouch 后保持按下状态

发布于 2024-12-23 09:11:01 字数 430 浏览 1 评论 0原文

我有一个列表视图,其中列表项具有以下背景可绘制

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/transparent" />
    <item android:state_pressed="true"
    android:drawable="@drawable/collection_item_gradient" />
</selector>

当按下列表项时,它会应用渐变。然而,如果用户随后发起多点触摸事件(例如,通过将额外的手指放在列表视图之外的某处),则列表项可能保持在按下状态,直到列表视图接收到另一个触摸事件。

有办法解决这个问题吗?

I have a list view where the list items have the following background drawable

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/transparent" />
    <item android:state_pressed="true"
    android:drawable="@drawable/collection_item_gradient" />
</selector>

When the list item is pressed, it applies the gradient. However, if the user then initiates a multitouch event (say by putting an additional finger down somewhere outside the list view) it is possible that the list item remains in the pressed state until another touch event is received by the list view.

Is there a way around this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笔芯 2024-12-30 09:11:01

这在某种程度上取决于您想要完成的任务...

如果您希望在用户按下两根手指时显示第三种背景类型,则没有可以直接引用来完成的可绘制状态。您必须合作触摸模式中不常用的现有状态之一(例如选定状态),并在列表项的对象上实现自定义触摸处理程序以设置该状态。例如,在每个列表项的视图中实现 onTouchEvent()(如果没有自定义类,则添加 onTouchListener()),查找 ACTION_POINTER_DOWN 事件告诉您第二根手指落下,并在视图上调用 setSelected() 来触发您为 android:state_selected 设置的可绘制对象在你的<选择器>

如果您只想在这种情况下清除状态,可以在检测到多于一根手指时取消触摸事件。在本例中,您需要自定义 ListView.onInterceptTouchEvent() 方法来查找 ACTION_PONTER_DOWN 事件并返回 true。这将教 ListView 使用第二根手指从其子项中窃取触摸,这将取消列表项的按下状态。在这种情况下,请确保回调到 super,这样就不会破坏 ListView 用于滚动等的所有现有逻辑。

请注意现有 >AbsListView 实现可能会妨碍您。例如,onIntercepTouchEvent() 的当前实现会查找 ACTION_POINTER_UP 以便将主触摸手指从第一个切换到第二个。如果您没有意识到,此行为可能会在您的自定义中导致奇怪的工件。

HTH。

It depends somewhat on what you are looking to accomplish...

If you are looking to display a third background type when the user has two fingers down, there is no drawable state that you can reference directly to accomplish. You would have to co-op one of the existing states that is not often used in touch mode (such as the selected state), and implement a custom touch handler on the list item's object to set that state. For example, implement onTouchEvent() in each list item's view (or add an onTouchListener() if you don't have a custom class), look for the ACTION_POINTER_DOWN event to tell you that a second finger came down, and call setSelected() on the view to trigger a drawable you have set for the android:state_selected in your <selector>.

If you just want the state to clear under this case, you can cancel the touch event when more than one finger is detected. In this case, you are looking at customizing the ListView.onInterceptTouchEvent() method to look for the ACTION_PONTER_DOWN event and return true. This will teach the ListView to steal the touch back from it's child item with a second finger, which will cancel the pressed state of the list item. Make sure to call back through to super in this case so you don't break all the existing logic ListView uses for scrolling, etc.

Beware of cases where the existing AbsListView implementation may get in your way here. For example, the current implementation of onIntercepTouchEvent() looks for ACTION_POINTER_UP in order to switch the main touch finger from the first to the second. This behavior may cause strange artifacts in your customization if you aren't aware of it.

HTH.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文