安卓选择器。按下后如何进入正常状态?

标签 android android-selector

我有一个卡片列表,当用户按下它们时,它会转到不同的 fragment 。

我在卡片上使用选择器来获得“按下状态”。我使用以下 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>

当我按下卡片时,如果下一个 fragment 需要 1 秒才能加载,卡片将保持“按下”状态,直到显示 fragment 。有人知道如何像最近的 Google 应用程序(Messenger、Gmail 等)那样做吗,即按下后,卡片进入“正常”状态,然后在 fragment 准备好后,显示 fragment 。

谢谢。

更新:问题似乎不在选择器中。如果我没有转到另一个 fragment 而是去显示另一个 Activity ,则卡片会进入正常状态。仅当转到 fragment 时。就像 fragment 占用了所有资源并阻止显示正常状态一样。

然而,如果我们转到 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();
}

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

最佳答案

尝试使用以下 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>

注意:Drawable 默认作为透明项目会很好,因为 CardView 为所有 android 版本提供默认背景

关于安卓选择器。按下后如何进入正常状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26991335/

相关文章:

android - 如何选择矢量绘图?

android - 高亮显示的 EditText 文本颜色

android - 如何实现类似Google Play Music的抽屉导航选择器

android - 在 Android 多用户应用程序中存储设置

android - 使用 GeoPoint 测量步行距离并向用户显示距离

android - onSaveInstanceState 没有保存我的值(onCreate input Bundle 始终为 null)

android - XML 选择器背景不适用于 2.3

android - 无法实例化类型 JsonFactory

java - 异常-将异常写入包裹