首页 > 代码库 > listview侧滑菜单swipemenulistview的简单使用
listview侧滑菜单swipemenulistview的简单使用
先看代码:
public class MainActivity extends AppCompatActivity {
private SwipeMenuListView list;
private ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = (SwipeMenuListView) findViewById(R.id.swipelist);
final List<String> listdata = http://www.mamicode.com/createListdata();
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, listdata);
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("Open");
// 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.drawable.ic_delete);
// add to menu
menu.addMenuItem(deleteItem);
SwipeMenuItem test = new SwipeMenuItem(
getApplicationContext());
// set item background
test.setBackground(new ColorDrawable(Color.rgb(0xC9, 0xC9,
0xCE)));
// set item width
test.setWidth(dp2px(90));
// set item title
test.setTitle("test");
// set item title fontsize
test.setTitleSize(18);
// set item title font color
// test.setTitleColor(Color.WHITE);
// add to menu
menu.addMenuItem(test);
}
};
// set creator
list.setMenuCreator(creator);
list.setAdapter(adapter);
list.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(int position, SwipeMenu menu, int index) {
switch (index){
case 0:
Log.e("tag", "点击打开----》: "+position );
break;
case 1:
listdata.remove(position);
adapter.notifyDataSetChanged();
break;
case 2:
Log.e("tag", "TEST----》: "+position );
break;
}
// return false;删除item后侧滑不关闭
return true;
}
});
}
private List<String> createListdata() {
ArrayList<String> list = new ArrayList<>();
for (int i = 0; i < 20; i++) {
list.add("东边日出西边雨,道是有晴却无晴"+"---->"+i);
}
return list;
}
private int dp2px(int dp) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
getResources().getDisplayMetrics());
}
}
注意要点:字体颜色,大小要设置,不然显示不出来,
demo下载:https://github.com/tiaodong/SwipeMenuListView
参考demo:http://www.jcodecraeer.com/a/opensource/2014/1108/1940.html
listview侧滑菜单swipemenulistview的简单使用
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。