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) {
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) {
opos = position;
omymsg = msgs.get(position);
if
(omymsg !=
null
) {
msgs.get(position).isread = !msgs.get(position).isread;
msgadapter.notifydatasetchanged();
}
}
private
void
createmenu() {
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);
}
};
listviewmymsgs.setmenucreator(creator);
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());
}
}