第三方开源库之 SwipeRecyclerView (滑动删除及拖拽)

SwipeRecyclerView 是基于 RecyclerView 的封装,提供了 Item 侧滑菜单、Item 滑动删除、Item 长按拖拽、添加HeaderView/FooterView、加载更多、Item点击监听等基本功能。

使用

第三方开源库之 SwipeRecyclerView (滑动删除及拖拽)_第1张图片
效果图
  1. 添加依赖
  • 如果你使用的是android support库,那么请添加下述依赖:
implementation 'com.yanzhenjie.recyclerview:support:1.3.2'
  • 如果你使用的是android x库,那么请添加下述依赖:
implementation 'com.yanzhenjie.recyclerview:x:1.3.2'
  1. 在布局的 xml 中加入 SwipeRecyclerView

  1. 分割线
    支持 Grid 形式和 Linear 形式,可以选择某个 ViewType 不画分割线:
// 默认构造,传入颜色即可。
ItemDecoration itemDecoration = new DefaultDecoration(color);

// 或者:颜色,宽,高,最后一个参数是不画分割线的ViewType,可以传入多个。
itemDecoration = new DefaultDecoration(color, width, height, excludeViewType);

// 或者:例如下面的123都是不画分割线的ViewType:
itemDecoration = new DefaultDecoration(color, width, height, 1, 2, 3);

recyclerView.setDecoration(itemDecoration);
  1. 逻辑代码
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.epro.test.R;
import com.epro.test.adapter.HomeAdapter;
import com.epro.test.bean.HomeBean;
import com.yanzhenjie.recyclerview.OnItemMenuClickListener;
import com.yanzhenjie.recyclerview.SwipeMenu;
import com.yanzhenjie.recyclerview.SwipeMenuBridge;
import com.yanzhenjie.recyclerview.SwipeMenuCreator;
import com.yanzhenjie.recyclerview.SwipeMenuItem;
import com.yanzhenjie.recyclerview.SwipeRecyclerView;

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

import butterknife.BindView;
import butterknife.ButterKnife;

public class LayoutActivity extends AppCompatActivity {
    @BindView(R.id.recyclerView)
    SwipeRecyclerView mRecyclerView;

    private HomeAdapter mAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_layout);

        ButterKnife.bind(this);

        initAdapter();

    }

    private void initAdapter() {
        mAdapter = new HomeAdapter(R.layout.item_home,getHomeItem());

        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                Toast.makeText(LayoutActivity.this,"click"+position,Toast.LENGTH_SHORT).show();
            }
        });

        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

        mRecyclerView.setSwipeMenuCreator(new SwipeMenuCreator() {
            @Override
            public void onCreateMenu(SwipeMenu leftMenu, SwipeMenu rightMenu, int position) {
                SwipeMenuItem deleteItem = new SwipeMenuItem(LayoutActivity.this);
                deleteItem.setBackgroundColor(Color.parseColor("#FF3D39"))
                        .setText("删除")
                        .setTextColor(Color.WHITE)
                        .setHeight(ViewGroup.LayoutParams.MATCH_PARENT)
                        .setWidth(170);

                rightMenu.addMenuItem(deleteItem);

            }
        });

        //菜单点击监听
        mRecyclerView.setOnItemMenuClickListener(new OnItemMenuClickListener() {
            @Override
            public void onItemClick(SwipeMenuBridge menuBridge, int adapterPosition) {
                menuBridge.closeMenu();
                mAdapter.getData().remove(adapterPosition);
                mAdapter.notifyDataSetChanged();
            }
        });

        mRecyclerView.setAdapter(mAdapter);
    }

    private List getHomeItem() {
        List list = Arrays.asList(
                new HomeBean("android", R.mipmap.ic_launcher),
                new HomeBean("ios", R.mipmap.ic_launcher),
                new HomeBean("java", R.mipmap.ic_launcher),
                new HomeBean("javascript", R.mipmap.ic_launcher),
                new HomeBean("php", R.mipmap.ic_launcher),
                new HomeBean("gyq", R.mipmap.ic_launcher),
                new HomeBean("ysh", R.mipmap.ic_launcher)
        );

        List infos = new ArrayList<>(list);
        return infos;
    }
}

你可能感兴趣的:(第三方开源库之 SwipeRecyclerView (滑动删除及拖拽))