如何为不同的list item呈现不同的菜单,本文实例就为大家介绍了Android仿微信或QQ滑动弹出编辑、删除菜单效果、增加下拉刷新等功能的实现,分享给大家供大家参考,具体内容如下
效果图:
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, SwipeMenu menu, int index)方法,对菜单事件进行处理;
4. 通过设置SwipeMenuListView的setOnRefreshListener方法,为菜单添加下拉刷新功能!
本文已被整理到了《Android微信开发教程汇总》,欢迎大家学习阅读。
如此这般,便可完美的实现的list item滑动编辑删除功能啦,希望大家喜欢。
您可能感兴趣的文章:Android制作微信app顶部menu菜单(ActionBar)Android仿今日头条APP实现下拉导航选择菜单效果Android App中DrawerLayout抽屉效果的菜单编写实例Android界面设计(APP设计趋势 左侧隐藏菜单右边显示content)Android实现顶部导航菜单左右滑动效果Android滑动优化高仿QQ6.0侧滑菜单(滑动优化)Android实现自定义滑动式抽屉效果菜单android RecyclerView侧滑菜单,滑动删除,长按拖拽,下拉刷新上拉加载Android利用滑动菜单框架实现滑动菜单效果Android实现美团APP的底部滑动菜单