亲宝软件园·资讯

展开

Android微信功能 Android仿微信滑动弹出编辑、删除菜单效果、增加下拉刷新功能

无缘公子 人气:0
想了解Android仿微信滑动弹出编辑、删除菜单效果、增加下拉刷新功能的相关内容吗,无缘公子在本文为您仔细讲解Android微信功能的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Android微信滑动弹出编辑,Android删除菜单效果,Android增加下拉刷新,下面大家一起来学习吧。

如何为不同的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滑动功能》

加载全部内容

相关教程
猜你喜欢
用户评论