Android + ListView background set background while scrolling?

I have a ListView that is populated through an ArrayAdapter. Inside the adapter, I set the background color of the view depending on the conditional. It works, but as you scroll through the remaining lines, apply this color. Here is the 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;
    }
} 
+3
source share
1 answer

This is the default behavior for ListView. It can be overridden by setting the cacheColorHint transparency. Just add

android:cacheColorHint="#00000000"

in your xml file.

For more information, see ListView Backgrounds . Here is an excerpt:

, , , , , . (. SetCacheColorHint (int)) XML, android: cacheColorHint. , # 00000000. android: cacheColorHint = "# 00000000", XML

EDIT: , convertView, , , (- ). , , , , . , reset , . - :

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

, , , , , convertView. .

+7

All Articles