突出在Android的列表视图中选择的项目 [英] highlighting the selected item in the listview in android

查看:175
本文介绍了突出在Android的列表视图中选择的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有1列表视图 contactslist 。我写的code突出显示在 ListView中选定的项目。这是工作。当我点击1个项目被突出显示该项目,但问题是,如果我点击其他项目则强调这一点。我只想高亮显示所选项目。在previous选择将要离去时,我点击另一个项目。

  arg1.setBackgroundResource(R.drawable.highlighter);
 

这是在code使用以高亮显示所选项目的点击监听器。 plz帮助我。

更新
我设置的行的背景在适配器:

 公众诠释[]颜色=新INT [] {0xFFedf5ff,为0xFFFFFFFF};
公众诠释colorPos;

[...]
colorPos =位置%colors.length;
row.setBackgroundColor(颜色[colorPos]);
 

解决方案

列表视图默认情况下不具有 choiceMode 设置(它被设置为),所以目前的选择是不是在视觉上表示。

要改变这种情况,你只需要你的的ListView choiceMode 属性设置为 singleChoice
如果您想自定义背景,在列表中选中的项目,您还应该设置 listSelector 属性。在那里,你可以指定不仅颜色,但可绘(图片,第一层/国家可绘制)。

 < ListView的机器人:ID =@ + ID / my_list
        机器人:choiceMode =singleChoice
        机器人:listSelector =@机器人:彩色/ darker_gray/>
 

如果您不使用的ListView 直接,但 ListActivity ,则需要这些属性进行设置

:从code,所以你应该与这些线延伸您的活动的的onCreate 方法

  getListView()setChoiceMode(ListView.CHOICE_MODE_SINGLE)。
。getListView()setSelector(android.R.color.darker_gray);
 

所以,如果你使用的是点击监听器更改所选行的背景,删除从code,并从上面使用适当的方法。

回复更新

如果你从你的getView方法来设置,而不是使用一个静态的色彩背景,应用状态列表绘制到该行背景duplicateParentState设置为true。这种方式将根据项目的当前状态,更改其显示:正常,关注,pressed等

I am having 1 list view contactslist. I wrote the code for highlighting the selected item in the ListView. It is working. When I click on 1 item it is highlighting that item but the problem is if I click on other item it is highlighting that too. I want to highlight the selected item only. The previous selection will have to gone when I click on another item.

arg1.setBackgroundResource(R.drawable.highlighter);

This is the code in the click listener using to highlight the selected item. plz help me.

Update
I'm setting the background of the rows in the adapter:

public int[] colors = new int[]{0xFFedf5ff, 0xFFFFFFFF}; 
public int colorPos; 

[...]
colorPos = position % colors.length; 
row.setBackgroundColor(colors[colorPos]);

解决方案

ListViews by default don't have a choiceMode set (it's set to none), so the current selection is not indicated visually.

To change this, you just need to set the choiceMode attribute of your ListView to singleChoice.
If you'd like custom background for the selected items in your list, you should also set the listSelector attribute. There you can specify not only colors, but drawables (images, layer-/state-drawables).

<ListView android:id="@+id/my_list"
        android:choiceMode="singleChoice" 
        android:listSelector="@android:color/darker_gray" />

If you don't use a ListView directly, but a ListActivity, then these attributes need to be set from code, so you should extend your activity's onCreate method with these lines:

getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
getListView().setSelector(android.R.color.darker_gray);

So if you were using a click listener to change the background of the selected row, remove that from your code, and use the proper method from above.

Reply to the update

If you set the background from your getView method, instead of using a static color, apply a state list drawable to the row background with duplicateParentState set to true. This way it will change its display based on the current state of the item: normal, focused, pressed, etc.

这篇关于突出在Android的列表视图中选择的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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