我有GridView
,其ChoiceMode为GridView.CHOICE_MODE_MULTIPLE_MODAL
。我希望选择的行为类似于您在Gallery应用程序中看到的内容(即,长按一个项目,所选项目具有半透明的前景色以指示它已被选中,短按其他项目添加或将它们从选择中删除。)
我已经创建了一个状态选择器:
<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>
和网格项背景的不同变体。