一:效果图、
二:实现步骤:
1.在项目build.gradle里面添加包。
compile 'com.baoyz.swipemenulistview:library:1.3.0'
2.xml布局文件、
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
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>
3.activity的实现,都写好了注释,相信也不难看懂,所以就不用解释了、
package com.example.administrator.myapplication;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.Toast;
import com.baoyz.swipemenulistview.SwipeMenu;
import com.baoyz.swipemenulistview.SwipeMenuCreator;
import com.baoyz.swipemenulistview.SwipeMenuItem;
import com.baoyz.swipemenulistview.SwipeMenuListView;
public class MainActivity extends AppCompatActivity {
private Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
/**
* 创建SwipeMenuCreator
*/
SwipeMenuCreator creator = new SwipeMenuCreator() {
@Override
public void create(SwipeMenu menu) {
SwipeMenuItem openItem = new SwipeMenuItem(context);
openItem.setBackground(new ColorDrawable(Color.BLACK));
openItem.setWidth(dp2px(90));
openItem.setTitle("置顶");
openItem.setTitleSize(20);
openItem.setTitleColor(Color.WHITE);
menu.addMenuItem(openItem);
SwipeMenuItem deleteItem = new SwipeMenuItem(context);
deleteItem.setBackground(new ColorDrawable(Color.RED));
deleteItem.setWidth(dp2px(90));
deleteItem.setTitle("删除");
deleteItem.setTitleSize(20);
deleteItem.setTitleColor(Color.WHITE);
menu.addMenuItem(deleteItem);
// //可以加入图片
// SwipeMenuItem deleteItem1 = new SwipeMenuItem(context);
// deleteItem1.setBackground(new ColorDrawable(Color.LTGRAY));
// deleteItem1.setWidth(dp2px(90));
// deleteItem1.setIcon(android.R.drawable.ic_delete);
// menu.addMenuItem(deleteItem1);
}
};
/**
* 实例化
*/
SwipeMenuListView listView = (SwipeMenuListView) findViewById(R.id.listView);
listView.setMenuCreator(creator);
/**
* 监听事件
*/
listView.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(int position, SwipeMenu menu, int index) {
//index的值就是在SwipeMenu依次添加SwipeMenuItem顺序值,类似数组的下标。
//从0开始,依次是:0、1、2、3...
switch (index) {
case 0:
Toast.makeText(context, "置顶:" + position, Toast.LENGTH_SHORT).show();
break;
case 1:
Toast.makeText(context, "删除:" + position, Toast.LENGTH_SHORT).show();
break;
}
// false : 当用户触发其他地方的屏幕时候,自动收起菜单。
// true : 不改变已经打开菜单的样式,保持原样不收起。
return false;
}
});
// 监测用户在ListView的SwipeMenu侧滑事件。
listView.setOnSwipeListener(new SwipeMenuListView.OnSwipeListener() {
@Override
public void onSwipeStart(int pos) {
Log.d("位置:" + pos, "开始侧滑...");
}
@Override
public void onSwipeEnd(int pos) {
Log.d("位置:" + pos, "侧滑结束.");
}
});
/**
* 添加一点死数据
*/
String[] data = new String[15];
for (int i = 0; i < data.length; i++) {
data[i] = "消息:" + i;
}
/**
* 绑定adapter
*/
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data);
listView.setAdapter(adapter);
}
public int dp2px(float dipValue) {
final float scale = this.getResources().getDisplayMetrics().density;
return (int) (dipValue * scale + 0.5f);
}
}
---------------------就这么简单,大神勿喷、
---------------------最后附上源码:点击打开链接