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

时间:2022-02-21 07:56:54

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

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

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

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

2. 创建消息实体bean:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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 +
        ", java" id="highlighter_935481">
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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,创建滑动菜单:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
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滑动弹出菜单标记已读、未读消息功能啦,希望大家喜欢。

延伸 · 阅读

精彩推荐