先给工程添加依赖:
compile 'com.baoyz.swipemenulistview:library:1.3.0'
将Swipemenulistview添加到xml中:
<com.baoyz.swipemenulistview.SwipeMenuListView android:id="@+id/collect_lv" android:layout_width="match_parent" android:layout_height="match_parent"></com.baoyz.swipemenulistview.SwipeMenuListView>
创建一个SwipeMenuCreator来添加菜单项:
SwipeMenuCreator creator = new SwipeMenuCreator() {
@Override
public void create(SwipeMenu menu) {
// create "open" item
SwipeMenuItem openItem = new SwipeMenuItem(
getApplicationContext());
// set item background
openItem.setBackground(new ColorDrawable(Color.rgb(0xC9, 0xC9,
0xCE)));
// set item width
openItem.setWidth(dp2px(90));
// set item title
openItem.setTitle("置顶");
// set item title fontsize
openItem.setTitleSize(18);
// set item title font color
openItem.setTitleColor(Color.WHITE);
// add to menu
menu.addMenuItem(openItem);
// create "delete" item
SwipeMenuItem deleteItem = new SwipeMenuItem(
getApplicationContext());
// set item background
deleteItem.setBackground(new ColorDrawable(Color.rgb(0xF9,
0x3F, 0x25)));
// set item width
deleteItem.setWidth(dp2px(90));
// set a icon
deleteItem.setIcon(R.mipmap.ic_pop_delete_normal_night);
// add to menu
menu.addMenuItem(deleteItem);
}
};
// set creator
listview.setMenuCreator(creator);
添加点击事件的监听:
listview.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(int position, SwipeMenu menu, int index) { switch (index){ case 0: //open break; case 1: //delete break; } return false; } });
private int dp2px(int dp) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, getResources().getDisplayMetrics());
}