SwipeMenuListView是在github上的第三方开源项目,该项目在github上的链接地址是:https://github.com/baoyongzhang/SwipeMenuListView 。
下载后直接将项目包复制粘贴到需要的项目当中:
测试代码:
item.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" > <ImageView
android:id="@+id/imageView"
android:layout_width="60dp"
android:layout_height="60dp"
android:src="@drawable/ic_launcher" /> <TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/imageView"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/imageView"
android:gravity="center"
android:textSize="20sp" /> </RelativeLayout>
item.xml
activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" > <com.baoyz.swipemenulistview.SwipeMenuListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent" /> </LinearLayout>
MainActivity.java:
package com.zzw.testswipemenulistview; import java.util.ArrayList; import com.baoyz.swipemenulistview.SwipeMenu;
import com.baoyz.swipemenulistview.SwipeMenuCreator;
import com.baoyz.swipemenulistview.SwipeMenuItem;
import com.baoyz.swipemenulistview.SwipeMenuListView;
import com.baoyz.swipemenulistview.SwipeMenuListView.OnMenuItemClickListener;
import com.baoyz.swipemenulistview.SwipeMenuListView.OnSwipeListener; import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import android.widget.Toast; public class MainActivity extends Activity { private ArrayList<String> datas;
private ArrayAdapter adapter;
private SwipeMenuListView listView;
private Handler handler; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); handler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg.what == 0) { int position = (Integer) msg.obj;
String str = datas.get(position);
datas.remove(position);
datas.add(0, str);
adapter.notifyDataSetChanged();
Toast.makeText(getApplicationContext(), "置顶成功", 0).show(); } else if (msg.what == 1) { Toast.makeText(getApplicationContext(), "打开", 0).show(); } else if (msg.what == 2) { int position = (Integer) msg.obj;
datas.remove(position);
adapter.notifyDataSetChanged();
Toast.makeText(getApplicationContext(), "成功删除", 0).show(); }
}
}; // 添加测试数据
datas = new ArrayList<String>();
for (int i = 0; i <= 50; i++) {
datas.add("测试数据-->" + i);
} adapter = new MyAdapter(this, -1); SwipeMenuCreator creator = new SwipeMenuCreator() { @Override
public void create(SwipeMenu menu) {
// 设置置顶的菜单按钮,设置了图标和标题
SwipeMenuItem top = new SwipeMenuItem(getApplicationContext());
top.setBackground(new ColorDrawable(Color.LTGRAY));
top.setWidth(dp2px(90));
top.setTitle("置顶");
top.setTitleSize(20);
top.setIcon(R.drawable.ic_top);
top.setTitleColor(Color.RED);
menu.addMenuItem(top); // 设置进入的菜单按钮,只设置了标题
SwipeMenuItem openItem = new SwipeMenuItem(getApplicationContext());
openItem.setBackground(new ColorDrawable(Color.GREEN));
openItem.setWidth(dp2px(90));
openItem.setTitle("打开");
openItem.setTitleSize(20);
openItem.setTitleColor(Color.WHITE);
menu.addMenuItem(openItem); // 设置删除的菜单按钮,只设置了图标
SwipeMenuItem deleteItem = new SwipeMenuItem(getApplicationContext());
deleteItem.setWidth(dp2px(90));
deleteItem.setBackground(new ColorDrawable(Color.RED));
deleteItem.setIcon(R.drawable.ic_delete);
menu.addMenuItem(deleteItem);
}
}; listView = (SwipeMenuListView) findViewById(R.id.listView);
listView.setMenuCreator(creator); // 设置点击不同菜单时触法的事件
listView.setOnMenuItemClickListener(new OnMenuItemClickListener() { @Override
public boolean onMenuItemClick(int position, SwipeMenu menu, int index) {
// index的值就是在SwipeMenu依次添加SwipeMenuItem顺序值,类似数组的下标。
// 从0开始,依次是:0、1、2、3...
switch (index) {
case 0:
//使菜单平滑的关闭
listView.smoothCloseMenu(); Message msg0 = handler.obtainMessage();
msg0.what = 0;
msg0.obj = position;
//过1秒后发送,为了体现出菜单平滑关闭的效果
handler.sendMessageDelayed(msg0,1000);
String str = datas.get(position); break;
case 1: Message msg1 = handler.obtainMessage();
msg1.what = 1;
msg1.obj = position;
handler.sendMessage(msg1); break;
case 2:
listView.smoothCloseMenu(); Message msg2 = handler.obtainMessage();
msg2.what = 2;
msg2.obj = position;
handler.sendMessageDelayed(msg2, 1000); break;
}
// false : 当用户触发其他地方的屏幕时候,自动收起菜单。
// true : 不改变已经打开菜单的样式,保持原样不收起。
return false;
}
}); // 监测用户在ListView的SwipeMenu侧滑事件。
listView.setOnSwipeListener(new OnSwipeListener() { @Override
public void onSwipeStart(int position) {
Log.d("位置:--" + position, "开始发生侧滑");
} @Override
public void onSwipeEnd(int position) {
Log.d("位置:--" + position, "开始发生侧滑");
}
}); listView.setAdapter(adapter);
} private class MyAdapter extends ArrayAdapter { LayoutInflater inflater; public MyAdapter(Context context, int resource) {
super(context, resource);
inflater = LayoutInflater.from(context);
} @Override
public int getCount() {
return datas.size();
} @Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.item, null);
} TextView textView = (TextView) convertView.findViewById(R.id.textView);
textView.setText(datas.get(position)); return convertView;
} } public int dp2px(float dipValue) {
final float scale = this.getResources().getDisplayMetrics().density;
return (int) (dipValue * scale + 0.5f);
}
}