Android第三方控件SwipeDelMenuLayout实现侧滑删除

效果图:
Android第三方控件SwipeDelMenuLayout实现侧滑删除_第1张图片
步骤1. 在项目根build.gradle文件中增加JitPack仓库依赖。

 allprojects {
        repositories {
            ...
            maven { url "https://jitpack.io" }
        }
    }

步骤2. Add the dependency

   dependencies {
            compile 'com.github.mcxtzhang:SwipeDelMenuLayout:V1.3.0'
    }

步骤3. 列表控件的item中使用SwipeDelMenuLayout


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

步骤4.MainActivity绑定listView

package com.zhh.cehua;

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends Activity {
   private ListView listView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = (ListView)findViewById(R.id.listView);

        List listData=getData();
        ListViewAdapter adapter = new ListViewAdapter(listData,MainActivity.this);
        listView.setAdapter(adapter);
    }
    private List getData(){
        Listlist = new ArrayList<>();
        for(int i = 0; i <10 ; i++) {
            list.add("数据"+i);

        }
        return list;
    }

}

步骤5.适配器中实现删除

package com.zhh.cehua;

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.List;

/**
 * Created by 16838 on 2018/6/22.
 */
public class ListViewAdapter extends BaseAdapter{
    private Listlist;
    private Context context;


    public ListViewAdapter(Listlist,Context context){
        this.list=list;
        this.context=context;

    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if(convertView==null) {
            viewHolder=new ViewHolder();
            convertView =LayoutInflater.from(context).inflate(R.layout.item_layout,null);
            viewHolder.tvContent= (TextView) convertView.findViewById(R.id.tvContent);
            viewHolder.btnDelete= (Button) convertView.findViewById(R.id.btnDelete);
            viewHolder.swipeMenuLayout= (SwipeMenuLayout) convertView.findViewById(R.id.swipeMenuLayout);
            convertView.setTag(viewHolder);
        }else{
            viewHolder= (ViewHolder) convertView.getTag();
        }
        viewHolder.tvContent.setText(list.get(position)+"");

//      点击事件
        final ViewHolder  finalViewHolder= viewHolder;
        viewHolder.btnDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//              删除list中对应的数据
                list.remove(position);
//              重新绑定数据
                notifyDataSetChanged();
//              关闭侧滑菜单
                finalViewHolder.swipeMenuLayout.quickClose();
            }
        });

        return convertView;
    }
    class ViewHolder{
        SwipeMenuLayout swipeMenuLayout;
        TextView tvContent;
        Button btnDelete;
    }





    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
}

参考文章:
https://github.com/mcxtzhang/SwipeDelMenuLayout/blob/master/README-cn.md
源码下载:
MyButton—-cehua
https://download.csdn.net/download/zhaihaohao1/10493339

你可能感兴趣的:(控件,第三方)