如何为不同的List item呈现不同的菜单,本文实例就为大家介绍了AndroID仿微信或QQ滑动d出编辑、删除菜单效果、增加下拉刷新等功能的实现,分享给大家供大家参考,具体内容如下
效果图:
1. 下载开源项目,并将其中的liberary导入到自己的项目中:
2. 使用SwipeMenuListVIEw代替ListVIEw,在页面中布局:
<androID.support.v4.Widget.SwipeRefreshLayout androID:ID="@+ID/swipeRefreshMusic" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content"> <com.baoyz.swipemenuListvIEw.SwipeMenuListVIEw androID:ID="@+ID/ListVIEwMusic" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" /> </androID.support.v4.Widget.SwipeRefreshLayout>
注: SwipeRefreshLayout主要是为了给该列表增加下拉刷新功能!
3. 为该ListVIEw创建适配器,与正常的实现并无二致:
public class MusicAdapter extends BaseAdapter { private List<Music> mDatas; private Context mContext; public MusicAdapter(List<Music> mDatas,Context mContext) { this.mDatas = mDatas; this.mContext = mContext; } @OverrIDe public int getCount() { return mDatas.size(); } @OverrIDe public Music getItem(int position) { return mDatas.get(position); } @OverrIDe public long getItemID(int position) { return position; } @OverrIDe public VIEw getVIEw(int position,VIEw convertVIEw,VIEwGroup parent) { if (convertVIEw == null) { convertVIEw = VIEw.inflate(mContext,R.layout.List_item_music,null); new VIEwHolder(convertVIEw); } VIEwHolder holder = (VIEwHolder) convertVIEw.getTag(); Music music = mDatas.get(position); holder.textTitle.setText(music.Title); holder.textDesc.setText(music.desc); return convertVIEw; } class VIEwHolder { TextVIEw textTitle; TextVIEw textDesc; public VIEwHolder(VIEw vIEw) { textTitle = (TextVIEw) vIEw.findVIEwByID(R.ID.textTitle); textDesc = (TextVIEw) vIEw.findVIEwByID(R.ID.textDesc); vIEw.setTag(this); } } }
4. 为SwipeMenuListVIEw实现Adapter,并在之后,创建滑动菜单:
public class MainActivity extends AppCompatActivity { @VIEwInject(R.ID.ListVIEwMusic) private SwipeMenuListVIEw ListVIEwMusic; @VIEwInject(R.ID.swipeRefreshMusic) private SwipeRefreshLayout swipeRefreshMusic; private List<Music> musics; private MusicAdapter musicAdapter; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); x.vIEw().inject(this); musics = new ArrayList<>(); for (int i = 0; i < 4; i++) { Music music = new Music(); music.Title = "上邪 : " + i; music.desc = "我欲与君相知,长命无绝衰"; musics.add(music); } musicAdapter = new MusicAdapter(musics,this); ListVIEwMusic.setAdapter(musicAdapter); SwipeMenuCreator swipeMenuCreator = new SwipeMenuCreator() { @OverrIDe public voID create(SwipeMenu menu) { // create "edit" item SwipeMenuItem editItem = new SwipeMenuItem( getApplicationContext()); // set item background editItem.setBackground(new colorDrawable(color.rgb(0xC9,0xC9,0xCE))); // set item wIDth editItem.setWIDth(dp2px(90)); // set item Title editItem.setTitle("编辑"); // set item Title Fontsize editItem.setTitleSize(18); // set item Title Font color editItem.setTitlecolor(color.WHITE); // add to menu menu.addMenuItem(editItem); // create "edit" item SwipeMenuItem deleteItem = new SwipeMenuItem( getApplicationContext()); // set item background deleteItem.setBackground(new colorDrawable(color.parsecolor("#ff0000"))); // set item wIDth deleteItem.setWIDth(dp2px(90)); // set item Title deleteItem.setTitle("删除"); // set item Title Fontsize deleteItem.setTitleSize(18); // set item Title Font color deleteItem.setTitlecolor(color.WHITE); // add to menu menu.addMenuItem(deleteItem); } }; ListVIEwMusic.setMenuCreator(swipeMenuCreator); ListVIEwMusic.setonMenuItemClickListener(new SwipeMenuListVIEw.OnMenuItemClickListener() { @OverrIDe public boolean onMenuItemClick(int position,SwipeMenu menu,int index) { switch (index) {// index即是 *** 作menu添加的顺序 case 0: Toast.makeText(MainActivity.this,"编辑 : " + position,Toast.LENGTH_SHORT).show(); break; case 1: Toast.makeText(MainActivity.this,"删除 : " + position,Toast.LENGTH_SHORT).show(); musics.remove(position); musicAdapter.notifyDataSetChanged(); break; } return false; } }); swipeRefreshMusic.setonRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @OverrIDe public voID onRefresh() { swipeRefreshMusic.setRefreshing(true); new Handler().postDelayed(new Runnable() { @OverrIDe public voID run() { for (int i = 0; i < 4; i++) { Music music = new Music(); music.Title = "无缘 : " + i; music.desc = "风雨千山玉独行,天下倾心叹无缘"; musics.add(music); } musicAdapter.notifyDataSetChanged(); swipeRefreshMusic.setRefreshing(false); } },2000); } }); } private int dp2px(int dp) { return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,dp,getResources().getdisplayMetrics()); } }
注:
1. SwipeMenuCreator是为了给该Listitem创建菜单使用,需要实现该的接口中的create方法,在该方法中,可以自由创建菜单项,比如删除、编辑,可以设置菜单的背景颜色,字体颜色大小等;
2. 在创建SwipeMenuCreator实例后,需为列表设置菜单:ListVIEwMusic.setMenuCreator(swipeMenuCreator);
3. 通过设置SwipeMenuListVIEw的setonMenuItemClickListener方法,监听菜单的点击事件,通过onMenuItemClick(int position,int index)方法,对菜单事件进行处理;
4. 通过设置SwipeMenuListVIEw的setonRefreshListener方法,为菜单添加下拉刷新功能!
本文已被整理到了《Android微信开发教程汇总》,欢迎大家学习阅读。
如此这般,便可完美的实现的List item滑动编辑删除功能啦,希望大家喜欢。
总结以上是内存溢出为你收集整理的Android仿微信滑动d出编辑、删除菜单效果、增加下拉刷新功能全部内容,希望文章能够帮你解决Android仿微信滑动d出编辑、删除菜单效果、增加下拉刷新功能所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)