强制 ListView 项目保持“按下"状态.被点击后? [英] Force a ListView item to stay "pressed" after being clicked?

查看:20
本文介绍了强制 ListView 项目保持“按下"状态.被点击后?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ListView,当通过 onItemClick 侦听器单击项目行时,它会打开另一个活动.

I have a ListView that opens another activity when an item row is clicked via a onItemClick listener.

我希望该行从被点击到屏幕切换到新活动时一直保持按下状态.我认为这对用户来说会是一种更清晰的体验,并且您会在大多数打开/关闭对话框或切换活动的按钮上看到这种情况.

I would like that row to stay in its pressed state from the time it is clicked to the time the screen switches to a new activity. I think this would be a clearer experience for the user and you see this kind of thing with most buttons that open/close dialogs or switch activities.

我尝试在 onItemClick() 侦听器中设置 view.setPressed(true),但它似乎在按下状态恢复正常后不久被调用,因为它会轻微闪烁.

I tried setting view.setPressed(true) in the onItemClick() listener but it seems to get called just a moment after the press state changed back to normal because it flickers slightly.

例如:

mListView.setOnItemClickListener(new OnItemClickListener() {

   @Override
   public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
       view.setPressed(true);
       //start an activity
   }
});

该代码几乎可以正常工作,除了闪烁(用户按下列表项并变为按下状态,然后用户松开(完成点击)并在返回到来自 setPressed(true) 调用的按下状态)

That code almost works except for the flicker (User presses the list item and it turns to its pressed state, then user lets go (completing the click) and it turns back to its normal state for a split second before turning back to the pressed state from the setPressed(true) call)

有什么想法吗?

谢谢

我应该提到我正在使用 xml 可绘制选择器来定义列表背景的正常、按下、选择等状态.

I should mention that I am using an xml drawable selector to define the normal, pressed, selected, etc states for the background of the list.

推荐答案

我遇到了完全相同的问题,非常沮丧!

I had the exact same problem to much frustration!

但是,通过将 state_pressed 替换为 state_selected 很容易解决.在调用 onItemClick() 之前,按下状态仍会快速变化,但由于您的主题不依赖于 state_pressed,因此闪烁将不可见.在 onItemClick() 中设置 state_selected 后,项目将保持选中状态而不会发生任何闪烁.

It is, however, easily solved by substituting state_pressed with state_selected. The pressed state will still change rapidly before onItemClick() is called, but since your theme does not depend on state_pressed the flicker won't be visible. Once in onItemClick() you set state_selected and the item will stay selected without any flicker occurring.

这是我的选择器:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Selected Item -->
    <item android:state_selected="true"
        android:drawable="@color/list_pressed" />

    <!-- Default Item -->
    <item android:state_selected="false"
        android:drawable="@android:color/list_default" />
</selector>

还有我的 onListItemClick(...):

And my onListItemClick(...):

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    v.setSelected(true);
    //TODO: add further actions here
}

这篇关于强制 ListView 项目保持“按下"状态.被点击后?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆