本文主要是介绍Android中SwipeMenuListView的使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
标题:Android中SwipeMenuListView的使用
步骤:
1.在布局文件中初始化做SwipeMenuListView
2.在创建SwipeMenuCreator对象
3.创建SwipeMenuItem,设置背景、宽度、图标
4.将SwipeMenuItem添加到SwipeMenu中。
5.将SwipeMenuCreator,设置到SwipeMenuListView上
6.为listView设置setOnMenuItemClickListener事件监听器 回调onItemClick()
代码:
1.在布局中添加;
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context="${relativePackage}.${activityClass}" ><com.baoyz.swipemenulistview.SwipeMenuListViewandroid:id="@+id/listView"android:layout_width="match_parent"android:layout_height="match_parent" /></RelativeLayout>
2.在Activity中重写:
private SwipeMenuListView mListView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_list);mAppList = getPackageManager().getInstalledApplications(0);mListView = (SwipeMenuListView) findViewById(R.id.listView);mAdapter = new AppAdapter();mListView.setAdapter(mAdapter);// step 1. create a MenuCreatorSwipeMenuCreator creator = new SwipeMenuCreator() {@Overridepublic void create(SwipeMenu menu) {// create "open" itemSwipeMenuItem openItem = new SwipeMenuItem(getApplicationContext());// set item backgroundopenItem.setBackground(new ColorDrawable(Color.rgb(0xC9, 0xC9,0xCE)));// set item widthopenItem.setWidth(dp2px(90));// set item titleopenItem.setTitle("Open");// set item title fontsizeopenItem.setTitleSize(18);// set item title font coloropenItem.setTitleColor(Color.WHITE);// add to menumenu.addMenuItem(openItem);// create "delete" itemSwipeMenuItem deleteItem = new SwipeMenuItem(getApplicationContext());// set item backgrounddeleteItem.setBackground(new ColorDrawable(Color.rgb(0xF9,0x3F, 0x25)));// set item widthdeleteItem.setWidth(dp2px(90));// set a icondeleteItem.setIcon(R.drawable.ic_delete);// add to menumenu.addMenuItem(deleteItem);}};// set creatormListView.setMenuCreator(creator);// step 2. listener item click eventmListView.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {@Overridepublic boolean onMenuItemClick(int position, SwipeMenu menu, int index) {ApplicationInfo item = mAppList.get(position);switch (index) {case 0:// openopen(item);break;case 1:// delete
// delete(item);mAppList.remove(position);mAdapter.notifyDataSetChanged();break;}return false;}});
效果:
这篇关于Android中SwipeMenuListView的使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!