1, custom adapter in the configuration:
1 Public classMyadapterextendsbaseadapter{2 3 intMselect = 0;//Selected Items4 5 ......6 7 Public voidChangeselected (intPositon) {//Refresh Method8 if(Positon! =mselect) {9Mselect =Positon;Ten notifydatasetchanged (); One } A } - - the - PublicView GetView (intposition, View Convertview, ViewGroup parent) { - //if (convertview==null) { -Layoutinflater factory =Layoutinflater.from (context); +View V = (view) factory.inflate (R.layout.list_item,NULL); -TextView TV =(TextView) V.findviewbyid (R.ID.ITEM_TV); +Tv.settext ("Test"); A // } at if(mselect==position) { -V.setbackgroundresource (R.DRAWABLE.FC_BG);//Selected item Background -}Else{ -V.setbackgroundresource (r.drawable.bg);//Other Items Background - } - in returnv; - } to +}
2, in the activity of the application:
1 ...........2 3Madapter =Newmyadapter (...);4 5 Mylist.setadapter (madapter);6 7 //////////Tap Listen8 9Mylist.setonitemclicklistener (NewOnitemclicklistener () {Ten One @Override A Public voidOnitemclick (adapterview<?> Parent, view view,intPositionLongID) { -madapter.changeselected (position);//Refresh - the }......} - - ////////////////Select Listen - +Mylist.setonitemselectedlistener (NewOnitemselectedlistener () { - + @Override A Public voidOnitemselected (adapterview<?> Parent, view view,intPositionLongID) { atmadapter.changeselected (position);//Refresh - -}......}
Reference Original: Http://www.tuicool.com/articles/YFBVJf
"Android Notes" ListView tap or Select item to change item style or background