Highlight selected item on a gridview

▼魔方 西西 提交于 2019-12-05 03:08:18

问题


I am trying to highlight a selected item on a gridview (dinamically populated with an adapter), but it is not working.

I did research and i even tried to copy exactly the selector of other people and even the way that they put it on the gridview but i am not being able to put it working.

It just doesn't do anything. The background of each item is white (like i wanted), but when i press it (it it is on top of a textview or a imageview (part of the gridview item) it doesn't do anything. If i press out of the imageView or textview, it will do what i want.

EDIT : I have listeners for the images and the textviews, so it might be interfering with this selector ? How could i solve this problem?

Here is the code of the activity where i create the gridview :

  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));

Here is the xml of each item of this gridview : (where i define the background as the selector)

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

And here is my selector :

  <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" />

I might be missing something, I am new to Android, sorry if there is any rookie mistake.


回答1:


Create file selector.xml as:

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

Put your selector file in drawable folder as drawable/selector.xml and then in your 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" />



回答2:


try this :

          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();
                    }
                }
            });


来源:https://stackoverflow.com/questions/39204682/highlight-selected-item-on-a-gridview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!