Android选择器。如何在按下后返回正常状态?

3
我是一名有用的助手,可以为您翻译文本。
我有一个卡片列表,当用户按下它们时,会进入不同的片段。
我在卡片上使用选择器来设置“按下状态”。我使用以下XML代码:
<selector xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:state_enabled="true"
        android:state_pressed="true"
        android:drawable="@drawable/hatching" />

    <item
        android:state_enabled="true"
        android:state_focused="true"
        android:drawable="@color/transparent" />

    <item
        android:drawable="@color/transparent" />

</selector>

当我按下卡片时,如果下一个片段需要1秒钟才能加载,卡片会保持“按下”状态,直到片段显示出来。有人知道如何像最近的Google应用程序(Messenger,Gmail等)一样,在按下后,卡片进入“正常”状态,然后在片段准备好后显示片段。
谢谢。
更新:问题似乎不在选择器中。如果我不是去另一个片段,而是去显示另一个活动,卡片就会变成正常状态。只有当去到一个片段时才会这样。就像片段占用了所有资源,防止正常状态被显示一样。
然而,如果我们进入View类的onTouchEvent()方法,就会看到这段代码:
// Use a Runnable and post this rather than calling
// performClick directly. This lets other visual state
// of the view update before click actions start.
if (mPerformClick == null) {
  mPerformClick = new PerformClick();
}
if (!post(mPerformClick)) {
  performClick();
}

所以我不知道为什么正常状态没有显示...


尝试移除 android:state_focused="true" - SweetWisher ツ
谢谢,但是不行。 - Stéphane
2个回答

1
尝试这个...
<selector xmlns:android="http://schemas.android.com/apk/res/android" >

<item
    android:state_pressed="true"
    android:drawable="@drawable/hatching" />

<item
    android:drawable="@color/transparent" />


1
尝试使用以下选择器来选择 CardView。
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
          android:drawable="@drawable/card_on_press"/>
    <item android:state_focused="true" android:state_enabled="true"
          android:drawable="@drawable/card_on_focus"/>
    <item android:state_enabled="true"
          android:drawable="@drawable/card_default"/>
    <item android:state_enabled="false"
          android:drawable="@drawable/card_on_press"/>
</selector>

注意:作为卡片视图提供默认背景的所有Android版本,Drawable默认将作为透明项。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接