Android Imitation micro-letter slide pop-up edit, delete menu effect, increase Drop-down refresh function _android

Source: Internet
Author: User

How to present a different menu for different list items, this article introduces the Android imitation micro-letter or QQ slide pop-up edit, delete menu effect, increase Drop-down refresh functions such as the realization, share for everyone reference, specific content as follows

Effect Chart:

1. Download the Open source project and import the liberary into your project:

2. Use Swipemenulistview instead of ListView, layout in the page:

<android.support.v4.widget.swiperefreshlayout 
 android:id= "@+id/swiperefreshmusic" 
 android:layout_ Width= "Match_parent" 
 android:layout_height= "wrap_content" > 
 
 < Com.baoyz.swipemenulistview.SwipeMenuListView 
  android:id= "@+id/listviewmusic" 
  android:layout_width= " Match_parent " 
  android:layout_height=" match_parent "/> 
 
</ Android.support.v4.widget.swiperefreshlayout> 

Note: The main purpose of the swiperefreshlayout is to add drop-down refresh function to the list!

3. Creating adapters for the ListView is not the same as a normal implementation:

public class Musicadapter extends Baseadapter {private list<music> mdatas; 
 
 Private context Mcontext; 
  Public Musicadapter (list<music> mdatas, context mcontext) {this.mdatas = Mdatas; 
 This.mcontext = Mcontext; 
 @Override public int GetCount () {return mdatas.size (); 
 @Override public Music getitem (int position) {return mdatas.get (position); 
 @Override public long getitemid (int position) {return position; 
   @Override public View getview (int position, View Convertview, ViewGroup parent) {if (Convertview = null) { 
   Convertview = View.inflate (mcontext, r.layout.list_item_music, NULL); 
  New Viewholder (Convertview); 
 
  } Viewholder holder = (viewholder) convertview.gettag (); 
 
  Music music = mdatas.get (position); 
  Holder.textTitle.setText (Music.title); 
 
  Holder.textDesc.setText (MUSIC.DESC); 
 return convertview; 
  Class Viewholder {TextView texttitle; 
 
  TextView Textdesc; PuBlic viewholder (view view) {Texttitle = (TextView) View.findviewbyid (r.id.texttitle); 
   Textdesc = (TextView) View.findviewbyid (R.ID.TEXTDESC); 
  View.settag (this); 
 } 
 } 
}

4. Implement adapter for Swipemenulistview, and after that, create a sliding menu:

public class Mainactivity extends Appcompatactivity {@ViewInject (r.id.listviewmusic) Private Swipemenulistview lis 
 
 Tviewmusic; 
 
 @ViewInject (r.id.swiperefreshmusic) private swiperefreshlayout swiperefreshmusic; 
 
 Private list<music> musics; 
 
 Private Musicadapter Musicadapter; 
  @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); 
 
  Setcontentview (R.layout.activity_main); 
  X.view (). inject (this); 
 
  musics = new arraylist<> (); 
   for (int i = 0; i < 4; i++) {Music music = new Music (); 
   Music.title = "Upper Evil:" + i; 
   Music.desc = "I want to have a friend with you, a long and no failure"; 
  Musics.add (music); 
  } musicadapter = new Musicadapter (musics, this); 
 
  Listviewmusic.setadapter (Musicadapter); Swipemenucreator swipemenucreator = new Swipemenucreator () {@Override public void Create (Swipemenu menu) {/ /create "edit" item Swipemenuitem EditItem = new Swipemenuitem (Getapplicationcontext ()); 
    Set Item background edititem.setbackground (New Colordrawable (Color.rgb (0xc9, 0xc9, 0xCE)); 
    Set Item width edititem.setwidth (dp2px (90)); 
    Set Item title Edititem.settitle ("edit"); 
    Set Item title FontSize edititem.settitlesize (18); 
    Set Item title Font Color Edititem.settitlecolor (color.white); 
 
    Add to Menu Menu.addmenuitem (EditItem); 
    Create "edit" item Swipemenuitem DeleteItem = new Swipemenuitem (Getapplicationcontext ()); 
    Set Item background deleteitem.setbackground (new colordrawable Color.parsecolor ("#ff0000")); 
    Set Item width deleteitem.setwidth (dp2px (90)); 
    Set Item title deleteitem.settitle ("delete"); 
    Set Item title FontSize deleteitem.settitlesize (18); 
    Set Item title Font Color Deleteitem.settitlecolor (color.white); 
   Add to Menu Menu.addmenuitem (DeleteItem); 
 
  } 
  }; Listviewmusic.setmenucreator (SWipemenucreator); Listviewmusic.setonmenuitemclicklistener (New Swipemenulistview.onmenuitemclicklistener () {@Override public Boolea 
      n Onmenuitemclick (int position, swipemenu menu, int index) {switch (index) {//index is the Order of Action menu add case 0: 
      Toast.maketext (Mainactivity.this, "edit:" + position, Toast.length_short). Show (); 
     Break 
      Case 1:toast.maketext (mainactivity.this, "delete:" + position, Toast.length_short). Show (); 
      Musics.remove (position); 
      Musicadapter.notifydatasetchanged (); 
    Break 
   return false; 
 
  } 
  }); Swiperefreshmusic.setonrefreshlistener (New Swiperefreshlayout.onrefreshlistener () {@Override public void onrefres 
    H () {swiperefreshmusic.setrefreshing (true); New Handler (). postdelayed (New Runnable () {@Override public void run () {for (int i = 0; i < 4; i+ 
       +) {Music music = new Music (); 
       Music.title = "Missed:" + i; Music.desc = "The Wind and rain in the mountains of the lone Jade, the world heart Sigh missed"; 
      Musics.add (music); 
 
      } musicadapter.notifydatasetchanged (); 
     Swiperefreshmusic.setrefreshing (FALSE); 
 
   }, 2000); 
 } 
  }); private int dp2px (int dp) {return (int) typedvalue.applydimension (Typedvalue.complex_unit_dip, DP, Getresou 
 RCEs (). Getdisplaymetrics ()); 
 } 
}

Note:
1. Swipemenucreator is to create a menu for the ListItem, you need to implement the creation method in the interface, in this method, you can freely create menu items, such as delete, edit, you can set the background color of the menu, font color size, etc.;
2. After creating the Swipemenucreator instance, you need to set the menu for the list: Listviewmusic.setmenucreator (swipemenucreator);
3. By setting the Swipemenulistview Setonmenuitemclicklistener method, listen to the click events on the menu, through Onmenuitemclick (int position, swipemenu menu, int Index) method to handle the menu event;
4. By setting the Swipemenulistview Setonrefreshlistener method, add the Drop-down Refresh function to the menu!

This article has been organized into the "Android micro-credit Development tutorial Summary," Welcome to learn to read.

So, it can be the perfect implementation of the list item slide edit delete function, I hope you like.

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.