在网格视图上突出显示所选项目

Highlight selected item on a gridview

我试图在 gridview 上突出显示选定的项目(动态填充适配器),但它不起作用。

我做了研究,我什至试图完全复制其他人的选择器,甚至是他们把它放在 gridview 上的方式,但我无法让它工作。

它只是什么都不做。每个项目的背景都是白色的(就像我想要的那样),但是当我按下它时(它位于 textview 或 imageview(gridview 项目的一部分)之上)它什么都不做。如果我按下imageView 或 textview,它会做我想要的。

编辑:我有图像和文本视图的监听器,所以它可能会干扰这个选择器?我该如何解决这个问题?

这是我创建 gridview 的 activity 代码:

  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.highway_appselection_activity);
    gridView= (GridView) findViewById(R.id.HighwayGridView);

    gridView.setSelector(new ColorDrawable(Color.BLACK));

这是此 gridview 中每个项目的 xml:(我将背景定义为选择器)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/HighwayGridViewItem"
android:orientation="vertical"
android:background="@drawable/highway_appselection_selector"
android:padding="5dp">

<cm.aptoide.lite.HighwayCustomImageView
    android:layout_width="72dp"
    android:layout_height="72dp"
    android:id="@+id/highwayGridViewItemIcon"
    android:background="#FFFFFF"
    android:layout_gravity="center"
    android:scaleType="centerCrop"
    android:padding="5dp"
    android:clickable="true"/>

<!-- does this need to be my custom image view anymore? CHeck on that-->
<LinearLayout
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:orientation="horizontal">

    <TextView
       android:layout_width="wrap_content"
       android:layout_height="match_parent"
       android:id="@+id/highwayGridViewItemName"
       android:textColor="#000000"
       android:text="texto de teste"
       android:textSize="10sp"
        android:focusable="true"
       android:ellipsize="marquee"
       android:marqueeRepeatLimit="marquee_forever"
       android:layout_weight="2"
       android:textStyle="bold"
        android:paddingRight="5dp"
        android:layout_marginLeft="5dp"
        android:clickable="true"/>

   <ImageView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:src="@drawable/info_icon"
       android:padding="5dp"
       android:clickable="true"
       android:id="@+id/highwayGridViewItemInfoButton"/>

这是我的选择器:

  <selector xmlns:android="http://schemas.android.com/apk/res/android"  android:exitFadeDuration="@android:integer/config_mediumAnimTime">
<item android:state_enabled="true" android:state_pressed="true" android:drawable="@color/green_main_color" />
<item android:state_enabled="true" android:state_focused="true" android:drawable="@color/green_main_color" />
<item android:state_enabled="true" android:state_selected="true" android:drawable="@color/green_main_color" />
<item android:drawable="@android:color/white" />

我可能遗漏了一些东西,我是Android的新手,如果有任何菜鸟错误,请原谅。

创建文件 selector.xml 为:

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

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

</selector>

将您的选择器文件作为 drawable/selector.xml 放在 drawable 文件夹中,然后放在您的 gridView 中:

 <GridView 
    android:id="@+id/gridview" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:numColumns="auto_fit" 
    android:verticalSpacing="10dp" 
    android:horizontalSpacing="10dp" 
    android:stretchMode="columnWidth" 
    android:gravity="center"
    android:listSelector="@drawable/list_selector"
    android:scrollbars="none" />

试试这个:

          int nPrevSelGridItem = -1;
      gridview.setOnItemClickListener(new OnItemClickListener() {
                View viewPrev;

                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                                        int position, long id) {
                    try {
                        if (nPrevSelGridItem != -1) {
                            viewPrev = (View) gridview.getChildAt(nPrevSelGridItem);
                            viewPrev.setBackgroundColor(Color.WHITE);
                        }
                        nPrevSelGridItem = position;
                        if (nPrevSelGridItem == position) {
                            //View viewPrev = (View) gridview.getChildAt(nPrevSelGridItem);
                            view.setBackgroundColor(getResources().getColor(R.color.orange));
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });