强制ListView项留" pressed"之后被点击了? [英] Force a ListView item to stay "pressed" after being clicked?

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

问题描述

我有一个打开某项目行通过onItemClick监听点击时另一个活动一个ListView。

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

我想该行留在了pressed状态,从它被点击的时候屏幕切换到一个新的活动时间。我认为这将是为用户更清晰的体验,你会看到这种事情大部分按钮,打开/关闭对话框或开关的活动。

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.set pressed(真),但它似乎被调用只是片刻之后,preSS状态变回正常的,因为它闪烁略有下降。

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
   }
});

这code几乎适用除外闪烁(用户presses列表项,并将其变成了pressed状态,则用户放开(完点击),并返回其正常前二pssed设定$ P $义无反顾到pressed状态状态分裂(真)调用)

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绘制选择定义pssed正常,$ P $,选择等国名单的背景

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 。在pressed国家将仍然在 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项留&QUOT; pressed&QUOT;之后被点击了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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