SwipeMenuListView是在github上的第三方开源项目,该项目在github上的链接地址是:https://github.com/baoyongzhang/SwipeMenuListView 。
效果如图:
java代码:
package com.example.swipemenulistview;
import java.util.ArrayList;
import java.util.List;
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 android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
public class MainActivity extends Activity {
private SwipeMenuListView list_view;
private List<String> list;
private ArrayAdapter<String> adapter;
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
int pos = (Integer) msg.obj;
list.remove(pos);
adapter.notifyDataSetChanged();
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SwipeMenuCreator creator = new SwipeMenuCreator() {
@Override
public void create(SwipeMenu menu) {
SwipeMenuItem open_item = new SwipeMenuItem(getApplicationContext());
open_item.setBackground(new ColorDrawable(Color.RED));
open_item.setWidth(dp2px(90));
open_item.setTitle("Open");
open_item.setTitleSize(18);
open_item.setTitleColor(Color.WHITE);
menu.addMenuItem(open_item);
SwipeMenuItem delete_item = new SwipeMenuItem(getApplicationContext());
delete_item.setBackground(new ColorDrawable(Color.GRAY));
delete_item.setWidth(dp2px(90));
delete_item.setIcon(R.drawable.icon);
menu.addMenuItem(delete_item);
}
};
list_view = (SwipeMenuListView) findViewById(R.id.swipelistview);
list_view.setMenuCreator(creator);
list_view.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:
break;
case 1:
list_view.smoothCloseMenu();
Message msg = handler.obtainMessage();
msg.obj = position;
handler.sendMessageDelayed(msg, 200);
break;
default:
break;
}
// false : 当用户触发其他地方的屏幕时候,自动收起菜单。
// true : 不改变已经打开菜单的样式,保持原样不收起。
return false;
}
});
// 监测用户在ListView的SwipeMenu侧滑事件。
list_view.setOnSwipeListener(new SwipeMenuListView.OnSwipeListener() {
@Override
public void onSwipeStart(int pos) {
Log.d("位置:" + pos, "开始侧滑...");
}
@Override
public void onSwipeEnd(int pos) {
Log.d("位置:" + pos, "侧滑结束.");
}
});
list = new ArrayList<String>();
for (int i = 0; i < 30; i++) {
list.add("测试数据" + i);
}
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
list_view.setAdapter(adapter);
}
public int dp2px(float dipValue) {
final float scale = this.getResources().getDisplayMetrics().density;
return (int) (dipValue * scale + 0.5f);
}
}
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"
android:orientation="vertical" >
<com.baoyz.swipemenulistview.SwipeMenuListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>