0

I have a listview which contains 2 textViews and an ImageView. Whenever is pressed I want to the selected row to change a bit it's layout. For example change the color of the texts change the imageView add another one etc..I have added a custom list selector for the background to change. My question is which is the correct listener in which I should add:

            TextView title;
            TextView descr;

            title = (TextView) arg1.findViewById(R.id.title);
            descr = (TextView) arg1.findViewById(R.id.descr);

            title.setTextColor(Color.parseColor("#189dd9"));
            descr.setTextColor(Color.WHITE);

I added it to OnItemClickListener and it works correct after the item is clicked not while it is selected.

Libathos
  • 3,340
  • 5
  • 36
  • 61
  • Try to implement onClick listener for parent layout of list item in getView() on adpater and change list item as per your requirement. – Haresh Chhelana Jul 28 '14 at 08:38

2 Answers2

1

Inside adapter you can keep a track of which item is selected.

int selectedItemIndex;// ArrayList<Integer> selectedItems=new ArrayList<Integer>(); 

Now in getView just write if

if(position==selectedItemIndex){ 
   //Inflate different layoout
}else{
 //normal flow
 }

To set selected items you can set onItemClick Listener and set this variable in adapter or you can add an onClick listener inside getView itself and in it set this variable.

In case you want to have same layout just write different code inside if/else

vipul mittal
  • 17,343
  • 3
  • 41
  • 44
  • best way would be to set onItemClickListener on the listView and in the callback you will get position. Create a function in Adapter setSelectedItemIndex or something and call this by passing this position value. And after that call notifyDataSetChanged. – vipul mittal Jul 28 '14 at 09:01
0

I did using multiple selectors, according to this link I'm reposting the link just in case someone finds this thread first. cheers!

Community
  • 1
  • 1
Libathos
  • 3,340
  • 5
  • 36
  • 61