"Android Notes" ListView tap or Select item to change item style or background

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.