Android listview项目背景变化 [英] Android listview item background change

查看:30
本文介绍了Android listview项目背景变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个安卓列表视图.当我单击一个列表视图项目时,我想更改列表视图项目背景.

i have a android listview. i want to change listview item background when i click one listview item.

然后之前选择的项目必须回到默认背景.这意味着只需选择一项.

and then previous selected item must go back to default background. this means only one item has to be selected.

我已经搜索了很长时间.我可以使用 onItemClick() 更改所选项目的背景

i have searched it for a long time. i can change background of selected item using onItemClick()

但我无法更改之前选择的项目.例如,如果我选择第二个项目,它就被改变了.然后我选择第三项.我的天啊!它也改变了!我能为此做些什么.怎样才能得到以前的位置?

but i can't change previous selected item. for example, if i select second item, it was changed. and then i select third item. oh my god! it is changed too! what can i do for this. how can i get the previous position?

这是我的安卓代码.

private class ListViewItemClickListener implements
            AdapterView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {

            TextView title = (TextView) view.findViewById(R.id.title);
            title.setBackgroundResource(R.drawable.list_shape);

        }
    }

推荐答案

当我在一个类似的例子中有这个时,我有一个名为:

When I have this in a similar example I have a global field named:

selectedListItem;

这将在您的 onitemClickListener 中更新,然后前一个项目的背景将恢复为默认值.

This would be updated in your onitemClickListener and the previous item would then have it's background returned to the default.

所以要更新你的代码:

private class ListViewItemClickListener implements
        AdapterView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        //First update the previously selected item if one has been set
        if(selectedListItem!=null){
            TextView previousTitle = (TextView) selectedListItem.findViewById(R.id.title);
            previousTitle.setBackgroundResource(R.drawable.list_default_background);
        }
        //Then update the new one
        TextView title = (TextView) view.findViewById(R.id.title);
        title.setBackgroundResource(R.drawable.list_shape);
        selectedListItem = view;

    }
}

所以只需将 selectedListItem 初始化为适配器中的一个字段,并将 onClickListener 作为内部类,并使用默认背景绘制而不是 list_default_background.

So simply initalise selectedListItem as a field in your adapter with the onClickListener as an inner class and have your default background drawable instead of list_default_background .

或者,您可以跟踪位置编号而不是实际视图.

Alternatively you can track the position numbers instead of the actual view.

要为您的列表使用此方法,您还必须跟踪特定列表项的 ID 或对象实例.在我自己的解决方案中,在我的 ListAdapter 的 getView 方法中,我确保仅更新与正确项的 ID/实例匹配的列表项.使用您的代码方式,您还会发现,当您向下滚动此可见项目列表中相同位置的视图时,也会更新.这是因为列表视图指的是项目集合中的列表,其中每个集合对应于任何时候在屏幕上可见的项目.

To use this method for your list you will also have to keep track of an ID or object instance for your specific list item. In my own solution, in my ListAdapter's getView method I make sure only the list item that matches the ID/instance of the correct item is updated. With your code the way it is you will also find that when you scroll down the view at the same position in this list of visible items is also updated. This is because list view's refer to the list in sets of items, where each set corresponds to the items visible on the screen at any one time.

要更新单个特定项目,您将更适合使用其他答案中提到的选择器背景或指示器.

To update a singular, specific item you would be better suited to using a selector background or indicator as mentioned in the other answers.

HTH

这篇关于Android listview项目背景变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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