Android uidesign-The onitemlongclicklistener of the listview item listens to two types of events at the same time

Source: Internet
Author: User

For example, in the MP3 player's music playing list, click an item to start playing the music (onitemclicklistener), and press it to enter the editing mode (not to jump to another page, still on the same page ), at this time, onitemlongclicklistener is used, and then a long-pressed music entry is dragged again (the list order is changed). In this case, you need to block the last long-pressed event that enters the editing mode. The implementation code is as follows:

Package COM. example. longdoubleclickdemo; import Java. util. arraylist; import Java. util. list; import android. app. activity; import android. content. context; import android. OS. bundle; import android. view. layoutinflater; import android. view. view; import android. view. viewgroup; 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 mmusiclistview; private list <musicitem> musiclist; private myadapter madapter; private textview mtitlename; private Boolean meditmode; @ jsonvoid oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_main); mtitlename = (textview) Findviewbyid (R. id. titlebar); mmusiclistview = (listview) findviewbyid (R. id. music_list); initdata (); madapter = new myadapter (this); mmusiclistview. setadapter (madapter); initlisteners ();} private void initdata () {musiclist = new arraylist <musicitem> (); For (INT I = 0; I <20; I ++) {musiclist. add (setmusicitem (I) ;}} public Boolean geteditmode () {return meditmode;} public void seteditmode (Boolean meditmode) {This. meditmode = meditmode;} private void initlisteners () {mmusiclistview. setonitemclicklistener (New listview. onitemclicklistener () {@ overridepublic void onitemclick (adapterview <?> Parent, view, int position, long ID) {// toast. maketext (getapplicationcontext (), "default mode", toast. length_short ). show () ;}}); mmusiclistview. setonitemlongclicklistener (New listview. onitemlongclicklistener () {@ overridepublic Boolean onitemlongclick (adapterview <?> Parent, view, int position, long ID) {Boolean meditmode = geteditmode ();If (! Meditmode) {seteditmode (true); toast. maketext (getapplicationcontext (), "Enter edit mode", toast. length_short ). show (); mtitlename. settext ("edit mode");} else {toast. maketext (getapplicationcontext (), "edit" + position, toast. length_short ). show ();}Return false ;}}) ;}public musicitem setmusicitem (INT position) {musicitem = new musicitem (); musicitem. setmusictitle ("Music" + position); musicitem. setmusicdragicon (getresources (). getdrawable (R. drawable. change_order_grip); Return musicitem;} static class viewholder {public textview musicname; Public imageview musicdragicon;} class myadapter extends baseadapter {private layoutinflater minflater = NULL; Public myadapter (context) {This. minflater = layoutinflater. from (context) ;}@ overridepublic int getcount () {return musiclist. size () ;}@ overridepublic object getitem (INT position) {return musiclist. get (position) ;}@ overridepublic long getitemid (INT position) {return position ;}@ overridepublic view getview (INT position, view convertview, viewgroup parent) {viewholder holder = NULL; if (null = convertview) {holder = new viewholder (); convertview = minflater. inflate (R. layout. music_list_item, null); holder. musicname = (textview) convertview. findviewbyid (R. id. music_name); holder. musicdragicon = (imageview) convertview. findviewbyid (R. id. music_drag); convertview. settag (holder);} else {holder = (viewholder) convertview. gettag ();} holder. musicname. settext (musiclist. get (position ). getmusictitle (); holder. musicdragicon. setimagedrawable (musiclist. get (position ). getmusicdragicon (); Return convertview ;}}}

Running result: for the first time, press the trigger button to enter the editing mode. For example, if the purpose of the navigation bar is changed, and for the second or later, press the trigger item to execute other item time, for example, drag and drop to change the list Order (update later)

 

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.