2

I have created a custom adapter for a listview with a custom view.

Now I would like to change background of the listview items (bg color of layout in the custom view of an item) as I press a button.

Is it even possible? Is it possible to change for all listview items at once or I have to change one by one?

Arturs Vancans
  • 4,531
  • 14
  • 47
  • 76

3 Answers3

0

Try something like this

ListView mView;
...
int c = mView.getChildCount()
for(int i = 0; i < c; i++){
   View v = mView.getChildAt(i);
   v.setBackgroundColor(color);
}

As far as I know there is no way to set color for all child view. And if it would, there will be still iteration across all views in background.

snapix
  • 344
  • 2
  • 8
0

if you have inflated the "listitems" using "customview" then just bind the elements(e.g.: button) of customview in custom adapter and set the onclick listeners for them.

you can refer this: preserve state of elements inside listview's each row

In the my custom adapter,I have bind 1 TextView and 2 Buttons of my custom view and then implemented on click listeners.

I hope it will be helpful.

Community
  • 1
  • 1
Mehul Joisar
  • 15,348
  • 6
  • 48
  • 57
0

Alternatively to iterating over all children, you can do the color change in the binder and force the ListView to rebind the items (via the data change methods in the adapter). That way, you're not poking inside the ListView's view hierarchy, which is generally considered really bad practice.

Delyan
  • 8,881
  • 4
  • 37
  • 42