在上一篇《Android仿微信滑动d出编辑、删除菜单效果、增加下拉刷新功能》里,已经带着大家学习如何使用SwipeMenuListVIEw这一开源库实现滑动列表d出菜单,接下来,将进一步学习,如何为不同的List item呈现不同的菜单,此处我们做一个实例:AndroID 高仿QQ滑动d出菜单标记已读、未读消息,看下效果图:
1. 创建项目,并导入SwipeMenuListVIEw类库
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,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滑动d出菜单标记已读、未读消息功能啦,希望大家喜欢。
总结以上是内存溢出为你收集整理的Android仿QQ滑动d出菜单标记已读、未读消息全部内容,希望文章能够帮你解决Android仿QQ滑动d出菜单标记已读、未读消息所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)