的Andr​​oid如何突出一个选择列表中的 [英] Android how to highlight a selection in a list

查看:136
本文介绍了的Andr​​oid如何突出一个选择列表中的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的是非常简单的。

What I want to do is very simple.

我充满项ListFragment。如果我接触一个项目我想项目的文本或背景改变颜色。这意味着,这个项目被选中。现在,当我触摸其他项目我想最后一个项目变回正常,并选择新的。

I have a ListFragment filled with items. If I touch one item I want the text or background of the item to change color. This means that this item is selected. Now when I touch another item I want the last item to change back to normal and select the new one.

我怎样才能做到这一点?

How can I do this?

非常感谢你。

编辑:

这还记得上次意见工作,但依赖于访问最后一个视图要么设置它的状态或颜色的解决方案。现在这个工程。

The solutions that remember the last views work but are dependant on accessing the last view to either set it's state or color. Now this works.

不过,我有新的问题。我也有一个功能在列表中移动的项目。

However I have new problem. I also have a function for moving an item up in the list.

下面是:

public void MoveUp(){
    if (lastselected > 0){
        String item = adapter.getItem(lastselected);
        adapter.remove(item);
        adapter.insert(item, lastselected-1);           
        lastselected = lastselected -1;
        System.err.println("Is View NULL " + (getListView().getChildAt(lastselected) == null));
    }
}

问题(这就是为什么我坚持什么取消标记做中提出的第二个解决方案)是,在64项的最后一个项目列出getChildAt返回null功能。由于它变成空我既不能设置它的选中状态,也没有设置它的颜色了。

The problem (And this is why I insisted on "what to do in Unmark" in the second solution proposed) is that for the last items in a 64 item lists the function getChildAt returns null. Since it turns null I can neither set it's selected state nor set it's color back.

这是我的调试打印输出:

This is my debug printout:

07-21 18:30:54.490: WARN/System.err(6733): Is View NULL true
07-21 18:30:54.670: WARN/System.err(6733): Is View NULL true
07-21 18:30:54.860: WARN/System.err(6733): Is View NULL true
07-21 18:30:55.020: WARN/System.err(6733): Is View NULL true
07-21 18:30:55.180: WARN/System.err(6733): Is View NULL false
07-21 18:30:55.720: WARN/System.err(6733): Is View NULL false
07-21 18:30:55.930: WARN/System.err(6733): Is View NULL false
07-21 18:30:56.110: WARN/System.err(6733): Is View NULL false
07-21 18:30:56.340: WARN/System.err(6733): Is View NULL false

和所有我一直做的是pressing向上按钮。如果我能找到好歹这个函数返回一个非空指针那将是极大的AP preciated。谢谢

And all I kept doing is pressing the up button. If I can find someway for this function to return a non null pointer it would be greatly appreciated. Thanks

推荐答案

使用一个选择的(见StateList)

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item 
    android:state_pressed="true"
    android:drawable="@color/white" />
  <item
    android:state_selected="true"
    android:drawable="@color/selected_color" />
  <item 
    android:drawable="@color/default_color" />
</selector>

在后台为您LIST_ITEM把这个也就是上面的XML的引用。

In the background for your list_item put this which is a reference to the above XML.

android:background="@drawable/list_item_bg_selector"

这篇关于的Andr​​oid如何突出一个选择列表中的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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