本文介绍了Android的ListView控件+背景设置背景滚动时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个是通过一个ArrayAdapter填充一个ListView。在适配器,我设置视图背景颜色依赖于一个条件。它的工作原理,但在滚动其余的行采用这种颜色。下面是一些code:

 类DateAdapter扩展ArrayAdapter< D​​ateVO> {
    私人的ArrayList< D​​ateVO>项目;
    公众的ViewGroup ListViewItem的;    //构造
    公共DateAdapter(上下文的背景下,INT textViewResourceId,ArrayList的< D​​ateVO>项目){
        超(背景下,textViewResourceId,项目);
        this.items =物品;
    }    @覆盖
    公共查看getView(INT位置,查看convertView,父母的ViewGroup){
        尝试{
            如果(查看== NULL){
                LayoutInflater VI =(LayoutInflater)的getContext()getSystemService(Context.LAYOUT_INFLATER_SERVICE)。
                convertView = vi.inflate(R.layout.row,NULL);
            }            最后DateVO dateItem = items.get(位置);            如果(dateItem!= NULL){                //这是我的问题吗?确实位置的变化而滚动?
                如果(items.get(位置).getField()。等于(家)){
                    view.setBackgroundResource(R.drawable.list_bg_home);
                }
                ...
            }        }赶上(例外五){
            Log.i(ArrayAdapter.class.toString(),e.​​getMessage());
        }        返回视图。
    }
}


解决方案

这是一个ListView的默认行为。它可以通过cacheColorHint设置为透明覆盖。
只需添加,

 的android:cacheColorHint =#00000000

在XML文件中。

有关详细信息,你可以通过文章。
下面是一个摘录:

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). 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.That should solve your problem.

这篇关于Android的ListView控件+背景设置背景滚动时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 03:20