Android 侧滑删除(仿QQ)之SwipeDelMenuLayout

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中




    



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 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 mList;

    public MyAdapter(Context context, ArrayList 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




    

        

        

参考文章:

https://github.com/mcxtzhang/SwipeDelMenuLayout/blob/master/README-cn.md
https://blog.csdn.net/zhaihaohao1/article/details/80774749

你可能感兴趣的:(Android 侧滑删除(仿QQ)之SwipeDelMenuLayout)