Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> QQ好友列表向左滑動出現置頂、刪除--第三方開源--SwipeMenuListView,swipemenulistview

QQ好友列表向左滑動出現置頂、刪除--第三方開源--SwipeMenuListView,swipemenulistview

編輯:關於android開發

QQ好友列表向左滑動出現置頂、刪除--第三方開源--SwipeMenuListView,swipemenulistview


SwipeMenuListView是在github上的第三方開源項目,該項目在github上的鏈接地址是:https://github.com/baoyongzhang/SwipeMenuListView 。

下載後直接將項目包復制粘貼到需要的項目當中:

測試代碼:

item.xml:

1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" > 5 6 <ImageView 7 android:id="@+id/imageView" 8 android:layout_width="60dp" 9 android:layout_height="60dp" 10 android:src="@drawable/ic_launcher" /> 11 12 <TextView 13 android:id="@+id/textView" 14 android:layout_width="wrap_content" 15 android:layout_height="wrap_content" 16 android:layout_alignBottom="@+id/imageView" 17 android:layout_alignParentRight="true" 18 android:layout_alignParentTop="true" 19 android:layout_toRightOf="@+id/imageView" 20 android:gravity="center" 21 android:textSize="20sp" /> 22 23 </RelativeLayout> item.xml

activity_main.xml:

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent" >
 5 
 6     <com.baoyz.swipemenulistview.SwipeMenuListView
 7         android:id="@+id/listView"
 8         android:layout_width="match_parent"
 9         android:layout_height="match_parent" />
10 
11 </LinearLayout>

MainActivity.java:

  1 package com.zzw.testswipemenulistview;
  2 
  3 import java.util.ArrayList;
  4 
  5 import com.baoyz.swipemenulistview.SwipeMenu;
  6 import com.baoyz.swipemenulistview.SwipeMenuCreator;
  7 import com.baoyz.swipemenulistview.SwipeMenuItem;
  8 import com.baoyz.swipemenulistview.SwipeMenuListView;
  9 import com.baoyz.swipemenulistview.SwipeMenuListView.OnMenuItemClickListener;
 10 import com.baoyz.swipemenulistview.SwipeMenuListView.OnSwipeListener;
 11 
 12 import android.app.Activity;
 13 import android.content.Context;
 14 import android.graphics.Color;
 15 import android.graphics.drawable.ColorDrawable;
 16 import android.os.AsyncTask;
 17 import android.os.Bundle;
 18 import android.os.Handler;
 19 import android.os.Message;
 20 import android.util.Log;
 21 import android.view.LayoutInflater;
 22 import android.view.View;
 23 import android.view.ViewGroup;
 24 import android.widget.ArrayAdapter;
 25 import android.widget.TextView;
 26 import android.widget.Toast;
 27 
 28 public class MainActivity extends Activity {
 29 
 30     private ArrayList<String> datas;
 31     private ArrayAdapter adapter;
 32     private SwipeMenuListView listView;
 33     private Handler handler;
 34 
 35     @Override
 36     protected void onCreate(Bundle savedInstanceState) {
 37         super.onCreate(savedInstanceState);
 38         setContentView(R.layout.activity_main);
 39 
 40         handler = new Handler() {
 41             @Override
 42             public void handleMessage(Message msg) {
 43                 if (msg.what == 0) {
 44 
 45                     int position = (Integer) msg.obj;
 46                     String str = datas.get(position);
 47                     datas.remove(position);
 48                     datas.add(0, str);
 49                     adapter.notifyDataSetChanged();
 50                     Toast.makeText(getApplicationContext(), "置頂成功", 0).show();
 51 
 52                 } else if (msg.what == 1) {
 53 
 54                     Toast.makeText(getApplicationContext(), "打開", 0).show();
 55 
 56                 } else if (msg.what == 2) {
 57 
 58                     int position = (Integer) msg.obj;
 59                     datas.remove(position);
 60                     adapter.notifyDataSetChanged();
 61                     Toast.makeText(getApplicationContext(), "成功刪除", 0).show();
 62 
 63                 }
 64             }
 65         };
 66 
 67         // 添加測試數據
 68         datas = new ArrayList<String>();
 69         for (int i = 0; i <= 50; i++) {
 70             datas.add("測試數據-->" + i);
 71         }
 72 
 73         adapter = new MyAdapter(this, -1);
 74 
 75         SwipeMenuCreator creator = new SwipeMenuCreator() {
 76 
 77             @Override
 78             public void create(SwipeMenu menu) {
 79                 // 設置置頂的菜單按鈕,設置了圖標和標題
 80                 SwipeMenuItem top = new SwipeMenuItem(getApplicationContext());
 81                 top.setBackground(new ColorDrawable(Color.LTGRAY));
 82                 top.setWidth(dp2px(90));
 83                 top.setTitle("置頂");
 84                 top.setTitleSize(20);
 85                 top.setIcon(R.drawable.ic_top);
 86                 top.setTitleColor(Color.RED);
 87                 menu.addMenuItem(top);
 88 
 89                 // 設置進入的菜單按鈕,只設置了標題
 90                 SwipeMenuItem openItem = new SwipeMenuItem(getApplicationContext());
 91                 openItem.setBackground(new ColorDrawable(Color.GREEN));
 92                 openItem.setWidth(dp2px(90));
 93                 openItem.setTitle("打開");
 94                 openItem.setTitleSize(20);
 95                 openItem.setTitleColor(Color.WHITE);
 96                 menu.addMenuItem(openItem);
 97 
 98                 // 設置刪除的菜單按鈕,只設置了圖標
 99                 SwipeMenuItem deleteItem = new SwipeMenuItem(getApplicationContext());
100                 deleteItem.setWidth(dp2px(90));
101                 deleteItem.setBackground(new ColorDrawable(Color.RED));
102                 deleteItem.setIcon(R.drawable.ic_delete);
103                 menu.addMenuItem(deleteItem);
104             }
105         };
106 
107         listView = (SwipeMenuListView) findViewById(R.id.listView);
108         listView.setMenuCreator(creator);
109 
110         // 設置點擊不同菜單時觸法的事件
111         listView.setOnMenuItemClickListener(new OnMenuItemClickListener() {
112 
113             @Override
114             public boolean onMenuItemClick(int position, SwipeMenu menu, int index) {
115                 // index的值就是在SwipeMenu依次添加SwipeMenuItem順序值,類似數組的下標。
116                 // 從0開始,依次是:0、1、2、3...
117                 switch (index) {
118                 case 0:
119                     //使菜單平滑的關閉
120                     listView.smoothCloseMenu();
121 
122                     Message msg0 = handler.obtainMessage();
123                     msg0.what = 0;
124                     msg0.obj = position;
125                     //過1秒後發送,為了體現出菜單平滑關閉的效果
126                     handler.sendMessageDelayed(msg0,1000);
127                     String str = datas.get(position);
128 
129                     break;
130                 case 1:
131                     
132                     Message msg1 = handler.obtainMessage();
133                     msg1.what = 1;
134                     msg1.obj = position;
135                     handler.sendMessage(msg1);
136 
137                     break;
138                 case 2:
139                     listView.smoothCloseMenu();
140                     
141                     Message msg2 = handler.obtainMessage();
142                     msg2.what = 2;
143                     msg2.obj = position;
144                     handler.sendMessageDelayed(msg2, 1000);
145 
146                     break;
147                 }
148                 // false : 當用戶觸發其他地方的屏幕時候,自動收起菜單。
149                 // true : 不改變已經打開菜單的樣式,保持原樣不收起。
150                 return false;
151             }
152         });
153 
154         // 監測用戶在ListView的SwipeMenu側滑事件。
155         listView.setOnSwipeListener(new OnSwipeListener() {
156 
157             @Override
158             public void onSwipeStart(int position) {
159                 Log.d("位置:--" + position, "開始發生側滑");
160             }
161 
162             @Override
163             public void onSwipeEnd(int position) {
164                 Log.d("位置:--" + position, "開始發生側滑");
165             }
166         });
167 
168         listView.setAdapter(adapter);
169     }
170 
171     private class MyAdapter extends ArrayAdapter {
172 
173         LayoutInflater inflater;
174 
175         public MyAdapter(Context context, int resource) {
176             super(context, resource);
177             inflater = LayoutInflater.from(context);
178         }
179 
180         @Override
181         public int getCount() {
182             return datas.size();
183         }
184 
185         @Override
186         public View getView(int position, View convertView, ViewGroup parent) {
187             if (convertView == null) {
188                 convertView = inflater.inflate(R.layout.item, null);
189             }
190 
191             TextView textView = (TextView) convertView.findViewById(R.id.textView);
192             textView.setText(datas.get(position));
193 
194             return convertView;
195         }
196 
197     }
198 
199     public int dp2px(float dipValue) {
200         final float scale = this.getResources().getDisplayMetrics().density;
201         return (int) (dipValue * scale + 0.5f);
202     }
203 }

 

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