我有GridView
一个 ChoiceMode 为GridView.CHOICE_MODE_MULTIPLE_MODAL
. 我希望选择的行为与您在图库应用程序中看到的类似(即,长按某个项目,所选项目具有半透明的前景色以指示它已被选中,短按其他项目添加或将它们从选择中删除)。
我创建了一个状态选择器,如下所示:
<selector xmlns:android="http://schemas.android.com/apk/res/android" android:exitFadeDuration="@android:integer/config_mediumAnimTime">
<item android:drawable="@drawable/pressed_background" android:state_activated="true"/>
<item android:drawable="@drawable/pressed_background" android:state_checked="true"/>
<item android:drawable="@drawable/pressed_background" android:state_selected="true"/>
<item android:drawable="@drawable/pressed_background" android:state_pressed="true"/>
</selector>
这是pressed_background.xml
文件:
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="@color/pressed" />
</shape>
以及它所指的颜色:
<color name="pressed">#CC30d3dc</color>
我将它应用到我的网格中,如下所示:
<GridView
android:id="@+id/grid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/header"
android:layout_marginTop="8dp"
android:columnWidth="112dp"
android:divider="@null"
android:dividerHeight="0dp"
android:drawSelectorOnTop="true"
android:listSelector="@drawable/selectable_background"
android:numColumns="auto_fit"
android:stretchMode="columnWidth" />
这是XML
网格项目:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent" >
<ImageView
android:id="@+id/photo"
android:layout_width="@dimen/add_photos_image_size"
android:layout_height="@dimen/add_photos_image_size"
android:layout_centerInParent="true" />
</RelativeLayout>
问题是所选状态永远不会出现在网格项目上。当我开始长按时,按下状态颜色按预期显示在网格项目的顶部,但是一旦它被选中,颜色就会消失,并且没有迹象表明该项目处于选中状态。上下文操作栏按预期显示。我已经尝试了尽可能多的不同变体<selector>
和网格项目背景。