listview侧滑菜单swipemenulistview的简单使用

Posted 跳动的米

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 = 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的简单使用的主要内容,如果未能解决你的问题,请参考以下文章

Android给ListView添加侧滑菜单功能

RecyclerView侧滑菜单,滑动删除,长按拖拽,下拉刷新上拉加载

DrawerLayout侧滑菜单

android 如何实现边缘拖动侧滑?

史上最简单,一步集成侧滑(删除)菜单,高仿QQIOS。

Flutter -------- Drawer侧滑