Android-Listview items background color changing when scrolling

后端 未结 4 473
小蘑菇
小蘑菇 2021-01-14 17:41

My ListView contains two Textviews. In one row first one is for name and second one is for result. I need to change the background color of the re

4条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-14 18:28

    I think it is because list view recycles the view hence causing such problems .Try the following :

    @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View row = null;
            convertView = null;
            row = convertView;
    
            if (convertView == null) {
                LayoutInflater inflater = (LayoutInflater) _context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                row = inflater.inflate(R.layout.row, parent,
                        false);
                           // your code 
    
    
              }
         return row;
    }
    

    Since convert view and row view both are intialized as null .Hence rows will be inflated every time and preventing recycling of views .

    Link: Listview android recycling This links explains the mechanism of recycling views.

    • use the above code if wrong views are changing color.
    • if you are trying to say on touching views while scrolling background becomes black then use the following in list view

    android:cacheColorHint="#00000000"

提交回复
热议问题