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

滚动时Android ListView背景设置背景?,第1张

概述我有一个ListView,它通过ArrayAdapter填充.在适配器中,我根据条件设置视图背景颜色.它可以工作,但滚动剩余的行时采用这种颜色.这是一些代码: class DateAdapter extends ArrayAdapter<DateVO> { private ArrayList<DateVO> items; public ViewGroup listViewItem; 我有一个ListVIEw,它通过ArrayAdapter填充.在适配器中,我根据条件设置视图背景颜色.它可以工作,但滚动剩余的行时采用这种颜色.这是一些代码:

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;    }}
解决方法 这是ListVIEw的默认行为.可以通过将cachecolorHint设置为transparent来覆盖它.
只需添加,

androID:cachecolorHint="#00000000"

在您的xml文件中.

有关详细信息,请阅读ListView Backgrounds文章.
这是一段摘录:

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

编辑:作为convertVIEw传递的视图本质上是一个视图,它是列表视图的一部分,但不再可见(由于滚动).因此它实际上是您创建的视图,可能是您已设置自定义背景的视图.要解决此问题,请确保在不满足条件时重置背景.像这样的东西:

if(condition_satisfIEd) {    //set custom background for vIEw}else {    //set default background for vIEw    convertVIEw.setBackgroundResource(androID.R.drawable.List_selector_background);}

基本上,如果您的条件不满意,您将不得不撤消满足条件时正在执行的任何自定义 *** 作,因为您可能已收到旧的自定义视图作为convertVIEw.那应该可以解决你的问题.

总结

以上是内存溢出为你收集整理的滚动时Android ListView背景设置背景?全部内容,希望文章能够帮你解决滚动时Android ListView背景设置背景?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1121859.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-29
下一篇2022-05-29

发表评论

登录后才能评论

评论列表(0条)

    保存