1.效果图:
2.使用:
Step 1. 在项目根build.gradle文件中增加JitPack仓库依赖。
allprojects {
repositories {
maven { url "https://jitpack.io" }
}
}
2.Step 2. Add the dependency
dependencies {
implementation 'com.github.mcxtzhang:SwipeDelMenuLayout:V1.3.0'
}
3.activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
4.MainActivity中
package com.demo.drawer_delete;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private ListView mLv;
private ArrayList<String> mList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
mLv = (ListView) findViewById(R.id.lv);
mList = new ArrayList<>();
for (int i = 0; i < 20; i++) {
mList.add("我是安琪拉"+i);
}
MyAdapter adapter = new MyAdapter(this, mList);
mLv.setAdapter(adapter);
}
}
5.适配器MyAdapter
package com.demo.drawer_delete;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import com.mcxtzhang.swipemenulib.SwipeMenuLayout;
import java.util.ArrayList;
/**
* Created by $sl on 2019/5/4 13:37.
*/
public class MyAdapter extends BaseAdapter {
private Context mContext;
private ArrayList<String> mList;
public MyAdapter(Context context, ArrayList<String> list) {
mContext = context;
mList = list;
}
@Override
public int getCount() {
return mList.size();
}
@Override
public Object getItem(int position) {
return mList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if (convertView == null) {
viewHolder = new ViewHolder();
convertView = LayoutInflater.from(mContext).inflate(R.layout.layout_item, null);
viewHolder.mSwipeMenuLayout = (SwipeMenuLayout) convertView.findViewById(R.id.swipeMenuLayout);
viewHolder.mTvContent = (TextView) convertView.findViewById(R.id.tvContent);
viewHolder.mBtnDelete = (Button) convertView.findViewById(R.id.btnDelete);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.mTvContent.setText(mList.get(position) + "");
final ViewHolder lastViewHolder = viewHolder;
//对删除按钮监听
viewHolder.mBtnDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mList.remove(position);
//刷新适配器
notifyDataSetChanged();
//关闭左滑菜单
lastViewHolder.mSwipeMenuLayout.quickClose();
}
});
return convertView;
}
class ViewHolder {
SwipeMenuLayout mSwipeMenuLayout;
TextView mTvContent;
Button mBtnDelete;
}
}
6.layout_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">
<com.mcxtzhang.swipemenulib.SwipeMenuLayout
android:id="@+id/swipeMenuLayout"
android:layout_width="match_parent"
android:layout_height="50dp">
<TextView
android:id="@+id/tvContent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?android:attr/selectableItemBackground"
android:gravity="center"
android:text="项目中我是任意复杂的原ContentItem布局" />
<Button
android:id="@+id/btnDelete"
android:layout_width="100dp"
android:layout_height="match_parent"
android:background="#FF0000"
android:text="删除"
android:textColor="@android:color/white" />
</com.mcxtzhang.swipemenulib.SwipeMenuLayout>
</LinearLayout>
参考文章:
https://github.com/mcxtzhang/SwipeDelMenuLayout/blob/master/README-cn.md
https://blog.csdn.net/zhaihaohao1/article/details/80774749