Android的ListView项的背景变化背景、Android、ListView

2023-09-07 10:18:58 作者:旧梦失词¢

我有一个Android的列表视图。我想改变列表视图项的背景,当我单击其中一个列表视图项。

然后previous选择的项目必须返回到默认背景。这意味着只有一个项具有被选中。

我寻觅了很长的时间。我可以用onItemClick改变所选项目的背景()

但我不能改变previous选择的项目。例如,如果我选择第二项,它被改变。然后我选择第三项。哦,我的上帝!它也改变了!我能做些什么了这一点。我怎样才能获得previous位置?

这是我的android code。

 私有类ListViewItemClickListener工具
            AdapterView.OnItemClickListener {
        @覆盖
        公共无效onItemClick(适配器视图<>母公司视图中查看,INT位置,
                长ID){

            TextView的标题=(TextView中)view.findViewById(R.id.title);
            title.setBackgroundResource(R.drawable.list_shape);

        }
    }
 

解决方案

当我有这样一个类似的例子,我有一个名为全球现场:

  selectedListItem;
 
Android添加程序背景颜色,Android小程序实现切换背景颜色

这会在你的onitemClickListener更新和previous项目便拥有它的背景返回默认。

因此​​,要更新您的code:

 私有类ListViewItemClickListener工具
        AdapterView.OnItemClickListener {
    @覆盖
    公共无效onItemClick(适配器视图<>母公司视图中查看,INT位置,
            长ID){
        //首先更新previously所选项目是否已设置
        如果(selectedListItem!= NULL){
            TextView的previousTitle =(TextView中)selectedListItem.findViewById(R.id.title);
            previousTitle.setBackgroundResource(R.drawable.list_default_background);
        }
        //然后更新新一
        TextView的标题=(TextView中)view.findViewById(R.id.title);
        title.setBackgroundResource(R.drawable.list_shape);
        selectedListItem =图。

    }
}
 

所以,简单地initalise selectedListItem 在与 onClickListener 适配器作为一个内部类的字段,有你的默认背景绘制,而不是 list_default_background

另外,您可以跟踪,而不是实际的视图位置编号。

编辑:

要为你的列表中使用这种方法,你还必须保持一个ID的曲目或对象实例为您的特定列表项。在我自己的解决方案,在我ListAdapter的getView方法我确保只有符合正确的项目的ID /实例列表项的更新。用c的方式你的$ C $是你还会发现,当你向下滚动视图在相同的位置在可见的项目列表中也将更新。这是因为,列表视图的参考该列表中项集,其中每个集合对应于在屏幕上可见的项目在任一时刻的

要更新,你会更适合使用中提到的其他答案一个选择背景或指标单一,特定项目。

心连心

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.

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?

here is my android code.

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;

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

So to update your code:

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;

    }
}

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.

EDIT:

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