java - How can I modify the background of a ListItem when it has been selected? -


I have a ListView with a group of ListItem. When user selects one item, then I I would like to change the background into an image. How can I complete it?

  listView.setOnItemClickListener (New OnItemClickListener () {@Override public void onItemClick (adapterview & lt;? & Gt; a, View v, int position, long id) {// I ListItem here How do I change the background?}});  

You can set your ListView in the adapter . What you have to do is use setBackgroundResource on see that you are coming back. I give you a brief example:

  // It considers your adapter (which is clearly a // custom A main adapter base adapter, curse adapter, array adapter, etc.) / / Is a getView method in every adapter that you have to overwrite / I think you know what I'm talking about public View getView (View theView; / // work with the scene before coming back Does set background to view.SetBackgroundResource (Rd Rawable.the_custom_background) Look at the return;}  

Note that I am using R.drawable.the_custom_background , which means that you have a small XML Do not worry, it's easier than sound. Create an XML file named the_custom_background.xml inside the res / drawable folder:

< Pre> & lt; xml version = "1.0" encoding = "utf-8" & gt; Selector xmlns: android = "http://schemas.android.com/apk/res/android" & Gt; & lt; item android: state_pressed = "true" Android: state_enabled = "true" android: drawable = "@ drawable / the_back Ground_color "/> & Lt; / Selector & gt; Note again, I am using @ drawable / the_background_color , so in the folder named res / drawable at the end Another downloadable the_background_color :

  & lt ;? XML version = "1.0" encoding = "UTF-8"? & Gt; & Lt; Size xmlns: android = "http://schemas.android.com/apk/res/android" & gt; & Lt; Solid Android: Color = "# FF 20000" /> & Lt; / Size & gt;  

I know this is very messy, but this is the Android way. You can also try to modify View inside setOnItemClickListener , but I think it is undesirable and difficult to implement.


Comments