首先在build.gradle中添加dependencies
compile 'com.baoyz.swipemenulistview:library:1.3.0'
效果
代码
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.kangfh.MainActivity">
<com.baoyz.swipemenulistview.SwipeMenuListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#eeeeee"
android:dividerHeight="15px">
</com.baoyz.swipemenulistview.SwipeMenuListView>
</RelativeLayout>
item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="70dp"
android:text="滑动listView"
android:gravity="center"
android:textSize="20sp"/>
</LinearLayout>
MainActivity
public class MainActivity extends AppCompatActivity {
private List<Map<String, Object>> datas = new ArrayList<>();
private Map<String,Object> map=new ArrayMap<>() ;
private Context context;
private SwipeMenuListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context=this;
listView = (SwipeMenuListView) findViewById(R.id.listView);
initData();
}
private void initData() {
datas.add(map);
datas.add(map);
datas.add(map);
datas.add(map);
listView.setAdapter(new Adapter(datas,context));
SwipeMenuCreator creator = new SwipeMenuCreator() {
@Override
public void create(SwipeMenu menu) {
SwipeMenuItem openItem = new SwipeMenuItem(
context.getApplicationContext());
openItem.setBackground(new ColorDrawable(Color.parseColor("#da2921")));
openItem.setWidth(260);
openItem.setTitle("删 除");
openItem.setTitleSize(20);
openItem.setTitleColor(Color.WHITE);
menu.addMenuItem(openItem);
SwipeMenuItem deleteItem = new SwipeMenuItem(
context.getApplicationContext());
deleteItem.setBackground(new ColorDrawable(Color.rgb(0xF9,
0x3F, 0x25)));
deleteItem.setWidth(260);
}
};
listView.setMenuCreator(creator);
}
public class Adapter extends BaseAdapter {
private List<Map<String, Object>> datas;
private Context context;
public Adapter(List<Map<String, Object>> datas, Context context) {
this.datas = datas;
this.context = context;
}
@Override
public int getCount() {
return datas == null ? 0 : datas.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.item, parent, false);
viewHolder = new ViewHolder(convertView);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
return convertView;
}
public class ViewHolder {
private TextView textView;
public View root;
public ViewHolder(View root) {
textView = (TextView) findViewById(R.id.textView);
this.root = root;
}
}
}
}
源代码下载