GridView突出显示所选项

时间:2015-04-28 09:48:43

标签: android xml gridview

我正在使用网格项目的自定义背景,并且正常情况下它可以工作,但是当我点击任何网格项目时(它不起作用)

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

对于ImageGallery我正在使用this教程并突出显示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项目:

enter image description here

2 个答案:

答案 0 :(得分:4)

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

<?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>

答案 1 :(得分:2)

为您的选择器编写可绘制文件,例如将其命名为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"