GridView 突出显示所选项目

GridView Highlight selected Item

我正在为网格项使用自定义背景,它正常工作,但是当我单击任何网格项时(它不起作用)

那么可能是什么原因,为什么当我点击任何网格项目时它不起作用,而它在正常模式下工作..

对于 ImageGallery,我使用 this tutorial and to highlight grid item following this 一个

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@color/blue" android:state_pressed="true" />
    <item android:drawable="@color/blue" android:state_selected="true" />
    <item android:drawable="@color/blue" android:state_focused="true" />
    <item android:drawable="@color/white" />

</selector>

网格项目布局xml:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:background="@drawable/grid_background"
     android:descendantFocusability="blocksDescendants"
     android:padding="5dp">

    <ImageView
        android:id="@+id/grid_item_image"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        android:scaleType="centerCrop">
    </ImageView>

    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:padding="1dip" >

        <ProgressBar
            android:id="@+id/loading"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:visibility="gone" />

    </FrameLayout>

</RelativeLayout>

这里是主网格xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000">

    <!-- The GridView to display picture's preview  -->
    <GridView
        android:id="@+id/grid_view"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:numColumns="auto_fit"
        android:gravity="center"
        android:stretchMode="columnWidth"    
        android:drawSelectorOnTop="true"       
        android:focusable="true"
        android:clickable="true"
        android:scrollbars="none"
        > 
    </GridView>

</RelativeLayout>

这是我正常得到的,甚至当我点击任何 GridView 项目时也是如此:

这可能对你有帮助 让你的选择器像这样

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@color/black" android:state_pressed="true" />
    <item android:drawable="@color/black" android:state_selected="true" />
    <item android:drawable="@color/black" android:state_focused="true" />
    <item android:drawable="@color/white" />

</selector>

和相对布局添加

  <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
      android:clickable="true"
     android:descendantFocusability="blocksDescendants" 
     android:background="@drawable/grid_background"
     android:padding="5dp">

    <ImageView
        android:id="@+id/grid_item_image"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        android:focusable="false"
        android:scaleType="centerCrop">
    </ImageView>

    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:focusable="false"
        android:padding="1dip" >

        <ProgressBar
            android:focusable="false"
            android:id="@+id/loading"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:visibility="gone" />

    </FrameLayout>

</RelativeLayout>

像这样为您的选择器编写可绘制文件 selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <shape>
            <solid android:color="@color/blue" />
        </shape>
    </item>

    <item android:state_pressed="false">
        <shape>
            <solid android:color="@android:color/transparent" />
        </shape>
    </item>
</selector>

然后在 gridview 的 xml 中添加以下两行

 android:drawSelectorOnTop="true"
 android:listSelector="@drawable/selector"