简体   繁体   中英

Changing the layout/color of a listView

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM