突出显示gridview上的选定项目 [英] Highlight selected item on a gridview

查看:111
本文介绍了突出显示gridview上的选定项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我做了研究,我甚至试过精确地复制其他人的选择器,甚至他们把它放在gridview上的方式,但我不能把它的工作。



它只是没有做任何事情。每个项目的背景都是白色的(就像我想要的),但是当我按下它(它位于textview或imageview的顶部(gridview项目的一部分))时,它不会执行任何操作。 imageView或textview,它会做我想做的。



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



以下是我创建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));

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

 <?xml version =1.0encoding =ut F-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/>

<! - 这是否需要成为我的自定义图像视图?在这方面挑战 - >
< LinearLayout
android:layout_width =match_parent
android:layout_height =wrap_content
android:orientation =horizo​​ntal>

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:/ / android / exit: 
< item android:state_enabled =trueandroid:state_pressed =trueandroid:drawable =@ color / green_main_color/>
< item android:state_enabled =trueandroid:state_focused =trueandroid:drawable =@ color / green_main_color/>
< item android:state_enabled =trueandroid:state_selected =trueandroid:drawable =@ color / green_main_color/>
< item android:drawable =@ android:color / white/>



我可能会错过一些东西,我是Android的新手,很抱歉,如果有新手的错误。 创建文件 selector.xml as:

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

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

< / selector>

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

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


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.

解决方案

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

这篇关于突出显示gridview上的选定项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆