在简单的ListView项目更改背景颜色 [英] Change item background color in simple listView

查看:151
本文介绍了在简单的ListView项目更改背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想点击时更改某个项目的背景颜色,在一个简单的ListView。这里是我的code:

I would like to change an item background color when clicked, in a simple listView. Here's my code:

boolean[] selectedItem = new boolean[listElement.length]
final ArrayList<String> list1 = new ArrayList<>();
Collections.addAll(list1, listElement);
final ListView mylist = (ListView) findViewById(R.id.listView);
final ArrayAdapter<String> adapter = new ArrayAdapter<>(context, android.R.layout.simple_list_item_1, list1);
mylist.setAdapter(adapter);
mylist.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {

            int firstVisiblePosition = mylist.getFirstVisiblePosition();
            int effectivePosition = pos - firstVisiblePosition;

            if (!selectedItem[pos]) {
                mylist.getChildAt(effectivePosition).setBackgroundColor(Color.parseColor("#66F44336"));
            } else {
                mylist.getChildAt(effectivePosition).setBackgroundColor(Color.parseColor("#EEEEEE"));
            }

           selectedItem[pos] = !selectedItem[pos];
        }
    });

在列表短(没有滚动参与)它的工作原理,当它长它不:点击的项目的背景颜色是变化的,但是当我开始滚动的的每个项目开始改变了,我找不到这些变化的逻辑,他们改变和扭转没有我,甚至接触他们,只是通过滚动,至极很奇怪,因为当onItemClick()被调用的颜色应该只改变,对不对?我缺少什么?

When the list is short (no scroll involved) it works, when it's long it does not: the background color of the clicked item does change, but when I start to scroll the background color of every item starts to change, and I can't find any logic in those changes, they change and reverse without me even touching them, just by scrolling, wich is strange since the color should only change when onItemClick() is called, right? What am I missing?

推荐答案

您错过了的ListView 外出时再使用其项目布局点屏幕(即您滚动列表)。

You're missing the point that the ListView re-uses its item layouts when they go out of screen (i.e. you scroll the list).

您需要保存为每个项目的背景和设定一次的观点要求。出现这种情况的内部的ListView 适配器 getView

You need to save the background for each of your item and set it once the view is requested. That happens inside of ListView adapter's getView.

有一个快速的解决将是在旅途中使用自定义适配器:

A quick fix would be to use a custom adapter on the go:

final boolean[] selectedItem = new boolean[listElement.length];

ArrayAdapter<String> adapter = new ArrayAdapter<String>(context,
        android.R.layout.simple_list_item_1, list1) {
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = super.getView(position, convertView, parent);
        if (selectedItem[position]) {
            view.setBackgroundColor(Color.parseColor("#66F44336"));
        } else {
            view.setBackgroundColor(Color.parseColor("#EEEEEE"));
        }
        return view;
    }
};

这没有错误检查,但你应该得到的想法。祝你好运!

This lacks error checking but you should get the idea. Good luck!

这篇关于在简单的ListView项目更改背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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