ListView保持选中状态? [英] ListView stay selected?

查看:262
本文介绍了ListView保持选中状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个充满项目的列表视图,用户选择一个项目后,它会亮起,然后恢复正常.有没有一种方法可以使用户在我的ListView中选择一个项目时,它保持选中状态并突出显示?

I have a list view full of items, after the users selects an item it lights up, and then it goes back to normal. Is there a way to make it so that when the user selects an item in my ListView it stays selected, and highlighted?

推荐答案

显然,消失的选择"是设计使然;这就是所谓的"触摸模式".我通读了该文档,但仍然不知道为什么他们认为这是一个好主意.我的猜测是,由于Android最初是为小屏幕设备设计的,因此他们希望您在屏幕上填充一个列表,然后在用户单击某个项目时,将其移至其他屏幕上的新列表.因此,用户不会知道Android丢失了对所选项目的跟踪.

Apparently the "disappearing selection" is by design; it's something called "touch mode". I read through that document and still I have no idea why they thought it was a good idea. My guess is that, since Android was originally designed for small-screen devices, they expected that you would fill the screen with a list and then, when the user clicks an item, move to a new list on a different screen. Thus, the user wouldn't be aware that Android lost track of the selected item.

但是,例如,如果您希望用户选择一个项目,然后在同一屏幕上显示有关该项目的信息,此行为将很烦人.如果选择消失了,用户应该如何知道他们单击了什么(当然,假设用户的注意力集中在金鱼身上)?

But this behavior is quite annoying if, for example, you want the user to select an item and then show information about that item on the same screen. If the selection disappears, how is the user supposed to know what they clicked (assuming of course that users have the attention span of a goldfish)?

一种可能的解决方案是将所有列表项都更改为单选按钮.我不太喜欢这种解决方案,因为它浪费了屏幕空间.我宁愿仅使用背景色来显示选择了哪个项目.到目前为止,我已经看到一种解决方案,但是它不是很完整或笼统.所以这是我的解决方案:

One possible solution is to change all the list items into radio buttons. I don't really like that solution because it wastes screen real estate. I'd rather just use the background color to show which item is selected. I have seen one solution so far but it is not quite complete or general. So here's my solution:

转到您的ListView元素和以下属性:android:choiceMode="singleChoice".我不完全确定这是做什么的(就其本身而言,它不允许用户选择任何东西),但是如果没有此属性,下面的代码将无法正常工作.

Go to your ListView element and the following attribute: android:choiceMode="singleChoice". I'm not entirely sure what this does (by itself, it doesn't allow the user to select anything) but without this attribute, the code below doesn't work.

它用于跟踪所选项目,还允许您在Java中模拟传递引用:

It is used to keep track of the selected item, and also allows you to simulate pass-by-reference in Java:

public class IntHolder {
    public int value;
    public IntHolder() {}
    public IntHolder(int v) { value = v; } 
}

3.将以下代码放在某处

我假设您将其放在活动"中,但实际上它可以在任何课程中使用:

3. Put the following code somewhere

I'll assume you put it in your Activity, but it could go in any class really:

static void setListItems(Context context, AdapterView listView, List listItems, final IntHolder selectedPosition)
{
    setListItems(context, listView, listItems, selectedPosition, 
                 android.R.layout.simple_list_item_1, 
                 android.R.layout.simple_spinner_dropdown_item);
}
static void setListItems(Context context, AdapterView listView, List listItems, final IntHolder selectedPosition, 
                         int list_item_id, int dropdown_id)
{
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> list, View lv, int position, long id) {
            selectedPosition.value = position;
        }
    });
    ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(context, list_item_id, listItems) { 
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View itemView = super.getView(position, convertView, parent);
            if (selectedPosition.value == position)
                itemView.setBackgroundColor(0xA0FF8000); // orange
            else
                itemView.setBackgroundColor(Color.TRANSPARENT);
            return itemView;
        }
    };
    adapter.setDropDownViewResource(dropdown_id);
    listView.setAdapter(adapter);
}

此代码有两件事:将列表项(例如List<String>)附加到ListView,并使用一些更改所选项目背景的代码覆盖ArrayAdapter.getView().

This code does two things: it attaches your list items (e.g. List<String>) to your ListView, and it overrides ArrayAdapter.getView() with some code that changes the background of the selected item.

例如:

ListView _list;
IntHolder _selectedItem = new IntHolder(-1); // nothing selected at first

@Override
protected void onCreate(Bundle savedInstanceState) {
    ...
    _list = (ListView)findViewById(R.id.list);
    List<String> items = Arrays.asList("Item 1", "Item 2", "Item 3");
    setListItems(this, _list, items, _selectedItem);
}

仅此而已!上面假设您要单选.我猜对getView()进行一些小的修改后,您也可以支持多选,但是您可能应该使用复选框.

That's all! The above assumes you want single selection. With some small modifications to getView(), you could support multi-selection too, I guess, but you should probably use checkboxes instead.

警告:此解决方案需要进一步开发.如果用户使用箭头键或按钮选择一个项目,则从IntHolder的角度来看该项目不会.如果用户按下未标记的按钮(该按钮的名称是什么?"Enter"?),则该项目将成为正式"选定的项目,但是您又遇到了另一个问题,因为如果用户再次使用箭头键,它将看起来很像就像选择了两个项目一样.如果您想出办法如何使IntHolder中的内部选择"与键盘选择"或其调用保持同步,请发表评论.它到底叫什么 ?

Warning: this solution needs further development. If the user uses arrow keys or buttons to select an item, that item will not be selected from the IntHolder's perspective. If the user presses the unlabeled button (what's the name of that button? "Enter"?) then the item will become "officially" selected but then you have another problem because if the user uses the arrow keys again, it will sort of look like two items are selected. Leave a comment if you figure out how to keep the "internal selection" in the IntHolder synchronized with the "keyboard selection" or whatever it's called. What is it called, anyway?

这篇关于ListView保持选中状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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