Android StateListDrawable 在 MultiTouch 后保持按下状态
我有一个列表视图,其中列表项具有以下背景可绘制
<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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这在某种程度上取决于您想要完成的任务...
如果您希望在用户按下两根手指时显示第三种背景类型,则没有可以直接引用来完成的可绘制状态。您必须合作触摸模式中不常用的现有状态之一(例如选定状态),并在列表项的对象上实现自定义触摸处理程序以设置该状态。例如,在每个列表项的视图中实现
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 anonTouchListener()
if you don't have a custom class), look for theACTION_POINTER_DOWN
event to tell you that a second finger came down, and callsetSelected()
on the view to trigger a drawable you have set for theandroid: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 theACTION_PONTER_DOWN
event and returntrue
. This will teach theListView
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 tosuper
in this case so you don't break all the existing logicListView
uses for scrolling, etc.Beware of cases where the existing
AbsListView
implementation may get in your way here. For example, the current implementation ofonIntercepTouchEvent()
looks forACTION_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.