http://dwtedx.sinaapp.com/itshare_290.html
本例子实现了滑动删除ListView的Itemdemo的效果、大家都知道、这种创意是来源于IOS的、左滑删除的功能、在Android上面实现比较麻烦、本例子中不仅实现了左滑删除功能、还实现了左滑赞、左滑分享、左滑收藏等功能、当然大家也可以根据自己项目的需求来修改功能、QQ和微信也实现了相同的功能、大家可以看看、先上程序运行的效果
怎么样、大家看了这个截图是不是很心动呀、而且在左滑的时候还配有简单的滑动动画呢、非常不错、下面一起来看看实现过程吧
初始化代码
@Override protected 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 MenuCreator 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); } }; // set creator mListView.setMenuCreator(creator); }
绑定左滑和删除事件
// step 2. listener item click event mListView.setOnMenuItemClickListener(new OnMenuItemClickListener() { @Override public void onMenuItemClick(int position, SwipeMenu menu, int index) { ApplicationInfo item = mAppList.get(position); switch (index) { case 0: // open open(item); break; case 1: // delete //delete(item); mAppList.remove(position); mAdapter.notifyDataSetChanged(); break; } } }); // set SwipeListener mListView.setOnSwipeListener(new OnSwipeListener() { @Override public void onSwipeStart(int position) { // swipe start } @Override public void onSwipeEnd(int position) { // swipe end } }); // other setting //listView.setCloseInterpolator(new BounceInterpolator()); // test item long click mListView.setOnItemLongClickListener(new OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(getApplicationContext(), position " long click", 0).show(); return false; } });
左滑的功能是封装好了的哈、上面贴出来的只是调用方法、更多使用方法、大家可以下载源代码慢慢研究
源代码下载链接: http://dwtedx.com/download.html?bdkey=s/1mgsUyow 密码: bale
时间: 2024-10-13 20:23:29