Android的ListView控件+背景设置背景滚动时? [英] Android + ListView background sets background while scrolling?

查看:124
本文介绍了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文件中。

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


  

要解决这个问题,你所要做的就是要么禁用缓存的颜色提示优化,如果使用非纯色背景,或设置提示到相应的纯色值。 cacheColorHint属性:您可以从code(见setCacheColorHint(INT))或preferably从XML,通过使用Android的做到这一点。要禁用优化,只需使用透明色#00000000。下面的截图显示了Android的列表:cacheColorHint =#00000000在XML布局文件设置


编辑:如convertView传递的观点是本质上的图是在列表视图中的一部分,但是不再可见(由于滚动)。因此,它实际上是你创造了一个观点,并且可能是你已经设置的自定义背景图。为了解决这个问题只需确保您重置背景,如果条件不满足。事情是这样的:

 如果(condition_satisfied){
    //为视图设置自定义背景
}
其他{
    //为视图设置默认背景
    convertView.setBackgroundResource(android.R.drawable.list_selector_background);
}

从本质上讲,如果你的条件不满足,你将不得不为撤消不管客户化你在做的时候你的条件得到满足,因为你可能已经收到了旧的自定义视图 convertView
这应该解决您的问题。

I have a ListView that's populated via an 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. It can be overridden by setting the cacheColorHint to transparent. Just add,

android:cacheColorHint="#00000000"

in your xml file.

For more details you can go through the ListView Backgrounds article. 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. To disable the optimization, simply use the transparent color #00000000. The following screenshot shows a list with android:cacheColorHint="#00000000" set in the XML layout file

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控件+背景设置背景滚动时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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