Android imitation QQ sliding pop-up menu Mark Read, unread message _android

Source: Internet
Author: User

In the previous "Android imitation micro-letter sliding pop-up Edit, delete menu effect, add Drop-down refresh function" , has taken you to learn how to use Swipemenulistview this open source library to implement the slide list pop-up menu, next, will further study, How to present different menu for different list item, here we do an example: Android high imitation qq sliding pop-up menu Mark read, unread message, look at the effect of the picture:


1. Create the project and import the Swipemenulistview class library

2. Create a message entity bean:

public class MSG {public 
 
  int id; 
 
  public String title; 
 
  Public String desc; 
 
  False is unread; True is read public 
  boolean isread; 
 
  @Override public 
  String toString () {return 
    "msg{" + 
        "id=" + ID + 
        ", title= '" + title + ' \ ' + 
        ", desc= ' "+ desc + ' \ ' + 
        ", isread= "+ Isread + 
        '} '; 
  } 
} 

Note: Through the Isread field, we distinguish the menu display "set as read" "Set to Unread";

3. Create the adapter of the list display, overriding the Getitemviewtype method:

Import android.app.Activity; 
Import Android.view.LayoutInflater; 
Import Android.view.View; 
Import Android.view.ViewGroup; 
Import Android.widget.BaseAdapter; 
Import Android.widget.ImageView; 
 
Import Android.widget.TextView; 
 
Import java.util.List; 
 
  /** * My msgs list adapter/public class Mymsgadapter extends Baseadapter {private activity mcontext; 
 
  Private Layoutinflater Minflater; 
 
  Private list<msg> Mdatas; 
    Public Mymsgadapter (activity context, list<msg> datas) {mcontext = context; 
    Minflater = Layoutinflater.from (Mcontext); 
  Mdatas = datas; 
  @Override public int GetCount () {Mdatas!= null? Mdatas.size (): 0); 
  @Override public Object getitem (int position) {return (Mdatas!= null mdatas.get (position): null); 
  @Override public long getitemid (int position) {return position; 
  @Override public int Getviewtypecount () {return 2; } @Override public int Getitemviewtype (int position) {MSG mymsg = mdatas.get (position); 
    Boolean isread = Mymsg.isread; 
    if (isread) {return 0; 
    else {return 1; } @Override Public View getview (final int position, View Convertview, ViewGroup parent) {Viewholder hol 
    Der = null; 
 
      if (Convertview = = null) {//dropdown item Layout Convertview = Minflater.inflate (r.layout.list_item_my_msg, NULL); 
 
      Holder = new Viewholder (); 
      Holder.img_msg = (ImageView) Convertview.findviewbyid (r.id.img_msg); 
      Holder.text_msg_user_name = (TextView) Convertview.findviewbyid (r.id.text_msg_user_name); 
 
      Holder.text_msg_infos = (TextView) Convertview.findviewbyid (R.id.text_msg_infos); 
 
    Convertview.settag (holder); 
    else {holder = (Viewholder) convertview.gettag (); 
 
    Final MSG mymsg = mdatas.get (position); if (mymsg!= null) {if (Mymsg.isread) {//unread Holder.img_msg.setImageREsource (R.MIPMAP.READED_MSG_IMG); 
      else {holder.img_msg.setImageResource (r.mipmap.unread_msg_img); 
      } holder.text_msg_user_name.setText (Mymsg.title); 
    Holder.text_msg_infos.setText (MYMSG.DESC); 
  return convertview; 
 
    Class Viewholder {ImageView img_msg; 
 
    TextView Text_msg_user_name; 
  TextView Text_msg_infos; 
 } 
}

Note: Here we rewrite the Getitemviewtype method in the parent class, in which we judge whether the return value is 0 or 1 according to the Isread field of MSG, and then we will determine the type of menu for the list item according to the return value.

4. According to Itemviewtype, create a sliding menu:

Import Android.graphics.Color; 
Import android.graphics.drawable.ColorDrawable; 
Import Android.os.Bundle; 
Import android.support.v7.app.AppCompatActivity; 
Import Android.util.TypedValue; 
Import Android.view.View; 
Import Android.widget.AdapterView; 
 
Import Android.widget.Toast; 
Import Com.apkfuns.logutils.LogUtils; 
Import Com.baoyz.swipemenulistview.SwipeMenu; 
Import Com.baoyz.swipemenulistview.SwipeMenuCreator; 
Import Com.baoyz.swipemenulistview.SwipeMenuItem; 
 
Import Com.baoyz.swipemenulistview.SwipeMenuListView; 
Import java.util.ArrayList; 
 
Import java.util.List; Import Butterknife. 
Bind; Import Butterknife. 
Butterknife; Import Butterknife. 
 
Onitemclick; public class Mainactivity extends Appcompatactivity {//Scenario list @Bind (R.ID.LISTVIEWMYMSGS) public SWIPEMENULISTVI 
 
  EW listviewmymsgs; 
 
  Private Mymsgadapter Msgadapter; 
 
  Message collection private list<msg> msgs; 
 
  The data to be deleted private MSG dmsg; 
 
  The data to be modified private int oPos; Private MSG OMymsg; 
    @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); 
 
    Setcontentview (R.layout.activity_main); 
 
    Butterknife.bind (this); 
 
    msgs = new arraylist<msg> (); 
    msg MSG1 = new msg (); 
    Msg1.id = 1; 
    Msg1.title = "upper evil"; 
    Msg1.desc = "I want to have a friend with you, a long and no failure"; 
 
    Msg1.isread = false; 
    msg MSG2 = new msg (); 
    Msg2.id = 2; 
    Msg2.title = "Love in the memory to find you"; 
    Msg2.desc = "If can hate you, full of hate you, even meet also want to avoid"; 
 
    Msg2.isread = true; 
    Msgs.add (MSG1); 
 
    Msgs.add (MSG2); 
    Msgadapter = new Mymsgadapter (this, msgs); 
 
    Listviewmymsgs.setadapter (Msgadapter); 
  CreateMenu (); /** * Delete a message * * @param position * * private void deletemsg (int position) {//Dodeletemsgre 
    Quest (String Id,handler mhandler, int reqcode) dmsg = msgs.get (position); 
 
      if (dmsg!= null) {Toast.maketext (mainactivity.this, "Delete:" + dmsg, Toast.length_short). Show (); Msgs.removE (dmsg); 
    Msgadapter.notifydatasetchanged (); }/** * Mark Read and unread * * @param position/private void readmsg (int position) {//DOREADMSGR 
    Equest (string ID, String isread, Handler mhandler, int reqcode) OPos = position; 
    Omymsg = Msgs.get (position); 
 
      if (omymsg!= null) {msgs.get (position). Isread =!msgs.get (position). Isread; 
    Msgadapter.notifydatasetchanged (); } private void CreateMenu () {//step 1. Create a menucreator swipemenucreator creator = new Swipemenu 
          Creator () {@Override public void Create (Swipemenu menu) {switch (Menu.getviewtype ()) { 
            Case 0://Unread createMenu1 (menu); 
          Break 
            Case 1://has been read createMenu2 (menu); 
        Break 
            } private void CreateMenu1 (Swipemenu menu) {Swipemenuitem Unreaditem = new Swipemenuitem ( 
        Getapplicationcontext ()); UnReaditem.setid (1); 
        Unreaditem.setbackground (New Colordrawable (Color.parsecolor ("#555555")); 
        Unreaditem.setwidth (DP2PX (90)); 
        Unreaditem.settitle ("marked as read"); 
        Unreaditem.settitlesize (16); 
        Unreaditem.settitlecolor (Color.parsecolor ("#FFFFFF")); 
 
        Menu.addmenuitem (Unreaditem); 
        Swipemenuitem DeleteItem = new Swipemenuitem (Getapplicationcontext ()); 
        Deleteitem.setid (0); 
        Deleteitem.setbackground (New Colordrawable (Color.parsecolor ("#EF4B3A")); 
        Deleteitem.setwidth (DP2PX (90)); 
        Deleteitem.settitle ("delete"); 
        Deleteitem.settitlesize (16); 
        Deleteitem.settitlecolor (Color.parsecolor ("#FFFFFF")); 
      Menu.addmenuitem (DeleteItem); 
            } private void CreateMenu2 (Swipemenu menu) {Swipemenuitem Readeditem = new Swipemenuitem ( 
        Getapplicationcontext ()); 
        Readeditem.setid (2); Readeditem.setbackground (New colordrawable (Color.parsecolor ("#555555")); 
        Readeditem.setwidth (DP2PX (90)); 
        Readeditem.settitle ("Mark unread"); 
        Readeditem.settitlesize (16); 
        Readeditem.settitlecolor (Color.parsecolor ("#FFFFFF")); 
 
        Menu.addmenuitem (Readeditem); 
        Swipemenuitem DeleteItem = new Swipemenuitem (Getapplicationcontext ()); 
        Deleteitem.setid (0); 
        Deleteitem.setbackground (New Colordrawable (Color.parsecolor ("#EF4B3A")); 
        Deleteitem.setwidth (DP2PX (90)); 
        Deleteitem.settitle ("delete"); 
        Deleteitem.settitlesize (16); 
        Deleteitem.settitlecolor (Color.parsecolor ("#FFFFFF")); 
      Menu.addmenuitem (DeleteItem); 
    } 
    }; 
 
    Set creator Listviewmymsgs.setmenucreator (creator); Step 2. Listener Item Click event Listviewmymsgs.setonmenuitemclicklistener (New Swipemenulistview.onmenuitemclicklistener () {@Override public boolean onmenuitemclick (int position, swipemenu menu, int index) {SWIPEMenuItem MenuItem = Menu.getmenuitem (index); 
        int id = Menuitem.getid (); 
            Switch (ID) {Case 0:logutils.e ("Delete:" + position); 
            DeleteMsg (position); 
          Break 
            Case 1:LOGUTILS.E ("Mark unread:" + position); 
            Readmsg (position); 
          Break 
            Case 2:LOGUTILS.E ("marked as read:" + position); 
            Readmsg (position); 
        Break 
      return false; 
  } 
    }); 
              @OnItemClick (R.ID.LISTVIEWMYMSGS) public void Onitemclick (adapterview<?> parent, view view, int position, 
    Long id) {msg mymsg = (msg) parent.getadapter (). GetItem (position); 
      if (mymsg!= null) {if (Mymsg.isread) {//If it is unread, it needs to be marked as read readmsg (position); }} private int dp2px (int dp) {return (int) typedvalue.applydimension (Typedvalue.complex_unit_dip, DP , Getresources (). GetdisplaymetrICS ()); 
 } 
}

Note: focus on the public void Create (Swipemenu menu) method in the Swipemenucreator interface, through the Menu.getviewtype () method, we can get the list Item ViewType, and this viewtype, so that we just defined ourselves in adapter, so at this point, just have to judge their own defined viewtype values, and according to ViewType to create a menu!

And so on, you can achieve similar QQ sliding pop-up menu tags have been read, unread message 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.