Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> 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滑動編輯刪除功能啦,希望大家喜歡。

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved