设置突出显示的项目在GridView控件在Android [英] Set highlighted item in GridView on Android

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

问题描述

有时候,我真的很惊讶地看到,简单的事情已经很难找到解决办法。

Sometimes I am really amazed to see that simple things have hard to find solutions.

我有一个 GridView控件 6列和多行。每个项目都是具有彩色背景正方形。当我选择一个项目,它被强调为 listSelector 设置

I have a GridView with 6 columns and multiple rows. Each item is a square having a color as background. When I select an item, it gets highlighted as the listSelector is set

<GridView
        android:id="@+id/listFontColors"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_gravity="center_horizontal"
        android:layout_margin="8dip"
        android:drawSelectorOnTop="true"
        android:gravity="center"
        android:horizontalSpacing="3dp"
        android:listSelector="@drawable/color_list_selected"
        android:numColumns="6"
        android:padding="0dip"
        android:scrollbars="none"
        android:stretchMode="columnWidth"
        android:verticalSpacing="3dp" />

当我打开的布局,包含 GridView控件我要强调所选项目的previous。至于其背后的适配器有一个的ArrayList&LT;对象&gt; 很容易找到需要强调什么位置。问题是执行实际的亮点。

When I open the layout, containing the GridView I want to highlight the previous selected item. As the adapter behind it has an ArrayList<Object> is easy to find what position needs to be highlighted. The problem is performing actual highlight.

我做了很多的测试:

listColors.requestFocus();
listColors.setSelection(9);

if (v != null) {
    v.requestFocus();
    v.setPressed(true);
    v.setSelected(true);
}
listColors.performItemClick(listColors, 9, listColors.getItemIdAtPosition(9));

这一切都不奏效。任何想法?

None of this worked. Any ideas?

推荐答案

最简单的方法是保存在位置,并检查您的适配器类中的内部 getView()方法。

The simplest way is to save the position and check it inside your Adapter class inside getView() method.

if(selected_position = position){
    view.setBackgroundResource(selected_resource_id);
}
else{
    view.setBackgroundResource(resource_id);
}

然后就叫 notifyDataSetChanged()你的适配器。

这篇关于设置突出显示的项目在GridView控件在Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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