你的位置:首页 > 操作系统

[操作系统]QQ好友列表向左滑动出现置顶、删除


SwipeMenuListView是在github上的第三方开源项目,该项目在github上的链接地址是:https://github.com/baoyongzhang/SwipeMenuListView 。

下载后直接将项目包复制粘贴到需要的项目当中:

测试代码:

item.

 1 <??> 2 <RelativeLayout ="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   <TextView13     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.

activity_main.

 1 <LinearLayout ="http://schemas.android.com/apk/res/android" 2   ="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.util.Log; 19 import android.view.LayoutInflater; 20 import android.view.View; 21 import android.view.ViewGroup; 22 import android.widget.ArrayAdapter; 23 import android.widget.TextView; 24 import android.widget.Toast; 25  26 public class MainActivity extends Activity { 27  28   private ArrayList<String> datas; 29   ArrayAdapter adapter; 30  31   @Override 32   protected void onCreate(Bundle savedInstanceState) { 33     super.onCreate(savedInstanceState); 34     setContentView(R.layout.activity_main); 35     // 添加测试数据 36     datas = new ArrayList<String>(); 37     for (int i = 0; i <= 50; i++) { 38       datas.add("测试数据-->" + i); 39     } 40  41     adapter = new MyAdapter(this, -1); 42  43     SwipeMenuCreator creator = new SwipeMenuCreator() { 44  45       @Override 46       public void create(SwipeMenu menu) { 47         // 设置置顶的菜单按钮,设置了图标和标题 48         SwipeMenuItem top = new SwipeMenuItem(getApplicationContext()); 49         top.setBackground(new ColorDrawable(Color.LTGRAY)); 50         top.setWidth(dp2px(90)); 51         top.setTitle("置顶"); 52         top.setTitleSize(20); 53         top.setIcon(R.drawable.ic_top); 54         top.setTitleColor(Color.RED); 55         menu.addMenuItem(top); 56  57         // 设置进入的菜单按钮,只设置了标题 58         SwipeMenuItem openItem = new SwipeMenuItem(getApplicationContext()); 59         openItem.setBackground(new ColorDrawable(Color.GREEN)); 60         openItem.setWidth(dp2px(90)); 61         openItem.setTitle("打开"); 62         openItem.setTitleSize(20); 63         openItem.setTitleColor(Color.WHITE); 64         menu.addMenuItem(openItem); 65  66         // 设置删除的菜单按钮,只设置了图标 67         SwipeMenuItem deleteItem = new SwipeMenuItem(getApplicationContext()); 68         deleteItem.setWidth(dp2px(90)); 69         deleteItem.setBackground(new ColorDrawable(Color.RED)); 70         deleteItem.setIcon(R.drawable.ic_delete); 71         menu.addMenuItem(deleteItem); 72       } 73     }; 74  75     SwipeMenuListView listView = (SwipeMenuListView) findViewById(R.id.listView); 76     listView.setMenuCreator(creator); 77  78     // 设置点击不同菜单时触法的事件 79     listView.setOnMenuItemClickListener(new OnMenuItemClickListener() { 80  81       @Override 82       public boolean onMenuItemClick(int position, SwipeMenu menu, int index) { 83         // index的值就是在SwipeMenu依次添加SwipeMenuItem顺序值,类似数组的下标。 84         // 从0开始,依次是:0、1、2、3... 85         switch (index) { 86         case 0: 87           String str = datas.get(position); 88           datas.remove(position); 89           datas.add(0, str); 90           adapter.notifyDataSetChanged(); 91           Toast.makeText(getApplicationContext(), "置顶成功", 0).show(); 92           break; 93         case 1: 94           Toast.makeText(getApplicationContext(), "打开", 0).show(); 95           break; 96         case 2: 97           datas.remove(position); 98           adapter.notifyDataSetChanged(); 99           Toast.makeText(getApplicationContext(), "成功删除", 0).show();100           break;101         }102         // false : 当用户触发其他地方的屏幕时候,自动收起菜单。103         // true : 不改变已经打开菜单的样式,保持原样不收起。104         return false;105       }106     });107 108     // 监测用户在ListView的SwipeMenu侧滑事件。109     listView.setOnSwipeListener(new OnSwipeListener() {110 111       @Override112       public void onSwipeStart(int position) {113         Log.d("位置:--" + position, "开始发生侧滑");114       }115 116       @Override117       public void onSwipeEnd(int position) {118         Log.d("位置:--" + position, "开始发生侧滑");119       }120     });121 122     listView.setAdapter(adapter);123   }124 125   private class MyAdapter extends ArrayAdapter {126 127     LayoutInflater inflater;128 129     public MyAdapter(Context context, int resource) {130       super(context, resource);131       inflater = LayoutInflater.from(context);132     }133 134     @Override135     public int getCount() {136       return datas.size();137     }138 139     @Override140     public View getView(int position, View convertView, ViewGroup parent) {141       if (convertView == null) {142         convertView = inflater.inflate(R.layout.item, null);143       }144 145       TextView textView = (TextView) convertView.findViewById(R.id.textView);146       textView.setText(datas.get(position));147 148       return convertView;149     }150 151   }152 153   public int dp2px(float dipValue) {154     final float scale = this.getResources().getDisplayMetrics().density;155     return (int) (dipValue * scale + 0.5f);156   }157 }