简体   繁体   English

Android + ListView背景在滚动时设置背景?

[英]Android + ListView background sets background while scrolling?

I have a ListView that's populated via an ArrayAdapter. 我有一个ListView,它通过ArrayAdapter填充。 Within the adapter, i set the view background color dependent on a conditional. 在适配器中,我根据条件设置视图背景颜色。 It works, but while scrolling the remaining rows adopt this color. 它可以工作,但滚动剩余的行时采用这种颜色。 Here's some code: 这是一些代码:

class DateAdapter extends ArrayAdapter<DateVO> {
    private ArrayList<DateVO> items;
    public ViewGroup listViewItem;

    //constructor
    public DateAdapter(Context context, int textViewResourceId, ArrayList<DateVO> items) {
        super(context, textViewResourceId, items);
        this.items = items;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        try {
            if (view == null) {
                LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = vi.inflate(R.layout.row, null);
            }

            final DateVO dateItem = items.get(position);

            if (dateItem != null) {

                //is this my issue here? does position change while scrolling?
                if(items.get(position).getField().equals("home")){
                    view.setBackgroundResource(R.drawable.list_bg_home);
                }
                ...
            }

        }catch (Exception e) {
            Log.i(ArrayAdapter.class.toString(), e.getMessage());
        }

        return view;
    }
} 

That is the default behavior of a ListView. 这是ListView的默认行为。 It can be overridden by setting the cacheColorHint to transparent. 可以通过将cacheColorHint设置为transparent来覆盖它。 Just add, 只需添加,

android:cacheColorHint="#00000000"

in your xml file. 在您的xml文件中。

For more details you can go through the ListView Backgrounds article. 有关更多详细信息,您可以阅读ListView背景文章。 Here is an excerpt: 这是一段摘录:

To fix this issue, all you have to do is either disable the cache color hint optimization, if you use a non-solid color background, or set the hint to the appropriate solid color value. 要解决此问题,您所要做的就是禁用缓存颜色提示优化,如果使用非纯色背景,或将提示设置为适当的纯色值。 You can do this from code (see setCacheColorHint(int)) or preferably from XML, by using the android:cacheColorHint attribute. 您可以使用android:cacheColorHint属性从代码(请参阅setCacheColorHint(int))或最好从XML执行此操作。 To disable the optimization, simply use the transparent color #00000000. 要禁用优化,只需使用透明色#00000000。 The following screenshot shows a list with android:cacheColorHint="#00000000" set in the XML layout file 以下屏幕截图显示了在XML布局文件中设置android:cacheColorHint =“#00000000”的列表

EDIT : The view passed as convertView is essentially a view which is a part of the list view, but isn't visible anymore (due to scrolling). 编辑:作为convertView传递的视图本质上是一个视图,它是列表视图的一部分,但不再可见(由于滚动)。 So it is actually a view you had created, and might be a view for which you had set the custom background. 因此它实际上是您创建的视图,可能是您已设置自定义背景的视图。 To solve this just make sure that you reset the background if the condition is not satisfied. 要解决此问题,请确保在不满足条件时重置背景。 Something like this : 像这样的东西:

if(condition_satisfied) {
    //set custom background for view
}
else {
    //set default background for view
    convertView.setBackgroundResource(android.R.drawable.list_selector_background);
}

Essentially if your condition is not satisfied you will have to undo whatever customisations you are doing when your condition is satisfied, because you might have received an old customised view as convertView . 基本上,如果您的条件不满意,您将不得不撤消满足条件时正在执行的任何自定义操作,因为您可能已收到旧的自定义视图作为convertView That should solve your problem. 那应该可以解决你的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM