【问题标题】:Highlight selected item on a gridview突出显示网格视图上的选定项目
【发布时间】:2016-08-29 11:03:51
【问题描述】:

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

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

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

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

这是我创建 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.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 新手,如果有任何菜鸟错误,请见谅。

【问题讨论】:

    标签: android android-layout gridview selector


    【解决方案1】:

    创建文件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" />
    

    【讨论】:

    • 但我已经有了。我的highway_appselection_selector 已经在drawable 文件夹中,我已经在我的gridview 上放置了android:background="@drawable/highway_appselection_selector" 这一行
    • @FilipeGonçalves 好的,请尝试更新您的 selctor.xml 并尝试一下。
    • 嘿@AndroidGeek 我试过了,但该代码没有突出显示。使用我拥有的那个,当我在 imageview 或 textview 之外单击时它会突出显示,但当我单击它们顶部时不会突出显示。
    • @FilipeGonçalves 尝试使用listSelector 代替背景。
    • @FilipeGonçalves 请注意,您必须在RelativeLayout 中有您的gridview 才能使listSelector 属性起作用。
    【解决方案2】:

    试试这个:

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

    【讨论】:

    • 试试上面的解决方案@Filipe Gonçalves
    • 那行不通,因为我必须让它访问 nPrevSelGridItem,它必须是最终的,然后如果它是最终的,你就不能做 nPrevSelGridItem = position
    • 这个解决方案完美地为我工作这就是为什么我为你推荐这个@FilipeGonçalves
    • 嗨 Anantham 感谢您的帮助。我设法通过从我拥有的 imageview 和 textview 中删除侦听器来解决它,并将选择器放在包含它们的线性布局上。感谢您的帮助!
    猜你喜欢
    • 2017-01-07
    • 2013-10-26
    • 1970-01-01
    • 1970-01-01
    • 2013-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多