欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android仿QQ滑动弹出菜单标记已读、未读消息

程序员文章站 2024-02-13 09:59:28
在上一篇《android仿微信滑动弹出编辑、删除菜单效果、增加下拉刷新功能》里,已经带着大家学习如何使用swipemenulistview这一开源库实现滑动列表弹出菜单,接...

在上一篇《android仿微信滑动弹出编辑、删除菜单效果、增加下拉刷新功能》里,已经带着大家学习如何使用swipemenulistview这一开源库实现滑动列表弹出菜单,接下来,将进一步学习,如何为不同的list item呈现不同的菜单,此处我们做一个实例:android 高仿qq滑动弹出菜单标记已读、未读消息,看下效果图:

Android仿QQ滑动弹出菜单标记已读、未读消息

1. 创建项目,并导入swipemenulistview类库

Android仿QQ滑动弹出菜单标记已读、未读消息

2. 创建消息实体bean:

public class msg { 
 
  public int id; 
 
  public string title; 
 
  public string desc; 
 
  // false是未读;true是已读 
  public boolean isread; 
 
  @override 
  public string tostring() { 
    return "msg{" + 
        "id=" + id + 
        ", title='" + title + '\'' + 
        ", desc='" + desc + '\'' + 
        ", isread=" + isread + 
        '}'; 
  } 
} 

注: 通过isread字段,我们区分菜单显示“设为已读”“设为未读”;

3. 创建列表显示的adapter,重写其中的getitemviewtype方法:

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() { 
    return (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 holder = null; 
    if (convertview == null) { 
      // 下拉项布局 
      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) {//未读 
        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; 
  } 
} 

注: 此处重写了父类中的getitemviewtype方法,该方法中,我们根据msg的isread字段来判断返回值的是0还是1; 之后,我们将根据该返回值,来判断list item的菜单是何种类型!

4. 根据itemviewtype,创建滑动菜单:

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 { 
 
  // 方案列表 
  @bind(r.id.listviewmymsgs) 
  public swipemenulistview listviewmymsgs; 
 
  private mymsgadapter msgadapter; 
 
  // 消息集合 
  private list<msg> msgs; 
 
  // 要删除的数据 
  private msg dmsg; 
 
  // 要修改的数据 
  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 = "上邪"; 
    msg1.desc = "我欲与君相知,长命无绝衰"; 
    msg1.isread = false; 
 
    msg msg2 = new msg(); 
    msg2.id = 2; 
    msg2.title = "爱在记忆中找你"; 
    msg2.desc = "如果可以恨你,全力痛恨你,连遇上亦要躲避"; 
    msg2.isread = true; 
 
    msgs.add(msg1); 
    msgs.add(msg2); 
 
    msgadapter = new mymsgadapter(this, msgs); 
    listviewmymsgs.setadapter(msgadapter); 
 
    createmenu(); 
  } 
 
  /** 
   * 删除某个消息 
   * 
   * @param position 
   */ 
  private void deletemsg(int position) { 
    // dodeletemsgrequest(string id,handler mhandler, int reqcode) 
    dmsg = msgs.get(position); 
    if (dmsg != null) { 
      toast.maketext(mainactivity.this, "删除 : " + dmsg, toast.length_short).show(); 
 
      msgs.remove(dmsg); 
      msgadapter.notifydatasetchanged(); 
    } 
  } 
 
  /** 
   * 标记已读和未读 
   * 
   * @param position 
   */ 
  private void readmsg(int position) { 
    //doreadmsgrequest(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 swipemenucreator() { 
      @override 
      public void create(swipemenu menu) { 
        switch (menu.getviewtype()) { 
          case 0:// 未读 
            createmenu1(menu); 
            break; 
          case 1:// 已读 
            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("标为已读"); 
        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("删除"); 
        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("标记未读"); 
        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("删除"); 
        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("删除 :" + position); 
            deletemsg(position); 
            break; 
          case 1: 
            logutils.e("标记未读 :" + position); 
            readmsg(position); 
            break; 
          case 2: 
            logutils.e("标为已读 :" + 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) { // 如果是未读,需要标记为已读 
        readmsg(position); 
      } 
    } 
  } 
 
  private int dp2px(int dp) { 
    return (int) typedvalue.applydimension(typedvalue.complex_unit_dip, dp, 
        getresources().getdisplaymetrics()); 
  } 
} 

注:着重看下swipemenucreator接口中的public void create(swipemenu menu)方法,通过menu.getviewtype()方法,我们可以得到该list item的viewtype,而这个viewtype,使我们刚刚在adapter自己定义的,所以此时,只需要判断自己所定义的viewtype值,并根据viewtype来创建菜单即可!

如此这般,便可实现类似qq滑动弹出菜单标记已读、未读消息功能啦,希望大家喜欢。