Android uidesign-selected items of listview

Source: Internet
Author: User

Sometimes the app will need to click an item to achieve the selected effect, for example, the effect of the content on the right side of the List implemented by fragment during pad, click an item on the left to highlight the selected item.

Sometimes it is unsuccessful to simply use setselected (Boolean B) or setselection (INT position). You must overwrite the adapter and process it in getview.

Package COM. example. selectitemtest; import Java. util. arraylist; import Java. util. hashmap; import Java. util. list; import Java. util. map; import android. app. activity; import android. content. context; import android. graphics. color; import android. OS. bundle; import android. view. layoutinflater; import android. view. view; import android. view. viewgroup; import android. widget. abslistview; import android. widget. adapterview; import android. widget. baseadapter; import android. widget. imageview; import android. widget. listview; import android. widget. textview; import android. widget. toast; public class mainactivity extends activity {private listview lv; private list <Map <string, Object> data; private myadapter adapter; @ override protected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_main); Lv = (listview) findviewbyid (R. id. LV); // obtain the data to be bound and set it to data = getdata (); adapter = new myadapter (this); LV. setadapter (adapter );LV. setchoicemode (abslistview. choice_mode_single );LV. setonitemclicklistener (New listview. onitemclicklistener () {@ override public void onitemclick (adapterview <?> Arg0, view arg1, int arg2, long arg3) {toast. maketext (getapplicationcontext (), "click position:" + arg2, Toast. length_short). Show ();Adapter. setselecteditem (arg2 );// Adapter. notifydatasetinvalidated () ;}}) ;}private list <Map <string, object >>getdata () {list <Map <string, object> List = new arraylist <Map <string, Object> (); Map <string, Object> map; For (INT I = 0; I <10; I ++) {map = new hashmap <string, Object> (); map. put ("IMG", R. drawable. ic_launcher); map. put ("title", "huarang"); map. put ("info", "motivation comes from interest... "); list. add (MAP) ;}return list ;}// viewholder static class viewholder {public imageview IMG; Public textview title; Public textview Info ;} public class myadapter extends baseadapter {private layoutinflater minflater = NULL; private int selecteditem =-1; private myadapter (context) {// load the layout according to the context. Here demo17activity itself is used, that is, this. minflater = layoutinflater. from (context) ;}@ override public int getcount () {// how many items are in the data set represented by this adapter. // return data as the number of entries in the dataset represented in this adapter. size () ;}@ override public object getitem (INT position) {// get the data item associated with the specified position in the data set. // obtain the return position of the data item corresponding to the specified index in the dataset ;}Public void setselecteditem (INT selecteditem) {This. selecteditem =Selecteditem ;}@ Override public long getitemid (INT position) {// get the row ID associated with the specified position in the list. // obtain the row ID return position corresponding to the specified index in the list;} // get a view that displays the data at the specified position in the data set. // obtain a view that specifies an index in the dataset to display the data @ override public view getview (INT position, view convertview, viewgroup parent) {viewholder holder = NULL; // If the cached convertview is empty, you need to create view if (convertview = NULL) {holder = new viewholder (); // load the layout convertview = minflater according to the custom item layout. inflate (R. layout. list_item, null); holder. IMG = (imageview) convertview. findviewbyid (R. id. IMG); holder. title = (textview) convertview. findviewbyid (R. id. TV); holder.info = (textview) convertview. findviewbyid (r.id.info); // Save the configured layout to the cache and set it in the tag to facilitate tag convertview. settag (holder);} else {holder = (viewholder) convertview. gettag ();} holder. IMG. setbackgroundresource (integer) data. get (position ). get ("IMG"); holder. title. settext (string) data. get (position ). get ("title"); holder.info. settext (string) data. get (position ). get ("info "));If (position = selecteditem) {// convertview. setbackgroundcolor (color. blue); // convertview. setselected (true); convertview. setbackgroundresource (R. drawable. all_listentry_left_selected);} else {// convertview. setbackgroundcolor (color. gray); // convertview. setselected (false); convertview. setbackgroundresource (R. drawable. lstview );}Return convertview ;}}}

The red mark in the Code is the focus, and LV. setchoicemode (abslistview. choice_mode_single); this sentence must be added

Defines the choice behavior for the list. By default, lists do not have any choice behavior (CHOICE_MODE_NONE). By setting the choicemodeCHOICE_MODE_SINGLE, The list allows up to one item to be in a chosen State. By setting the choicemodeCHOICE_MODE_MULTIPLE, The list allows any number of items to be chosen.

The implementation result is as follows:

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.