使用Android SwipeMenuRecyclerView实现RecycleView侧滑菜单(超实用)

学习使用大佬的SwipeMenuRecyclerView总结

导入依赖

Recyclerview

implementation 'com.android.support:recyclerview-v7:26.1.0'

SwipeMenuRecycleView

compile 'com.yanzhenjie:recyclerview-swipe:1.1.4'

注意:适配器和普通适配器使用一样,个人使用封装的RecycleView适配器

主Activity代码如下:

public class SwipeRecycleActivity extends AppCompatActivity {
    private List<String> list = new ArrayList<>();
    private ListAdapter listAdapter;
    private SwipeMenuCreator swipeMenuCreator;
    private SwipeMenuItemClickListener swipeMenuItemClickListener;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_swipe_recycle);
        initdata();//初始化数据
        setSwipeMenu();//设置菜单
        SwipeMenuRecyclerView swipeMenuRecyclerView = findViewById(R.id.rv_swipe);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        swipeMenuRecyclerView.setLayoutManager(linearLayoutManager);
        listAdapter = new ListAdapter(R.layout.layout_list_item, list);
        swipeMenuRecyclerView.setSwipeMenuCreator(swipeMenuCreator);
        swipeMenuRecyclerView.setSwipeMenuItemClickListener(swipeMenuItemClickListener);
        swipeMenuRecyclerView.setAdapter(listAdapter);
        listAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {

            }
        });
    }

    private void setSwipeMenu() {
        //                        .setImage(R.mipmap.icon_img)
        swipeMenuCreator = new SwipeMenuCreator() {
            @Override
            public void onCreateMenu(SwipeMenu swipeLeftMenu, SwipeMenu swipeRightMenu, int viewType) {
                int width = 200;
                int height = ViewGroup.LayoutParams.MATCH_PARENT;
                SwipeMenuItem deleteItem = new SwipeMenuItem(SwipeRecycleActivity.this)
                        .setBackground(R.drawable.ic_launcher_background)
//                        .setImage(R.mipmap.icon_img)
                        .setText("删除")
                        .setWidth(width)
                        .setHeight(height);
                SwipeMenuItem addItem=new SwipeMenuItem(SwipeRecycleActivity.this)
                        .setBackground(R.color.c10)
                        .setText("添加")
                        .setWidth(width)
                        .setHeight(height);
                SwipeMenuItem editItem=new SwipeMenuItem(SwipeRecycleActivity.this)
                        .setBackground(R.color.colorAccent)
                        .setText("编辑")
                        .setWidth(width)
                        .setHeight(height);
                swipeRightMenu.addMenuItem(addItem);
                swipeRightMenu.addMenuItem(editItem);
                swipeRightMenu.addMenuItem(deleteItem);
            }
        };
        swipeMenuItemClickListener = new SwipeMenuItemClickListener() {
            @Override
            public void onItemClick(SwipeMenuBridge menuBridge) {
                menuBridge.closeMenu();
                int direction = menuBridge.getDirection();//左边还是右边菜单
                int adapterPosition = menuBridge.getAdapterPosition(); // ecyclerView的Item的position。
                int position = menuBridge.getPosition();   // 菜单在左边或右边添加几个菜单的下标
                if (direction == SwipeMenuRecyclerView.RIGHT_DIRECTION) {
                    if (position == 0) {
                        Toast.makeText(SwipeRecycleActivity.this, "添加新条目", Toast.LENGTH_LONG).show();
                        list.add("添加-=-=-=-=-=-=-=-=数据");
                        listAdapter.notifyDataSetChanged();
                    } else if(position==1){
                        Toast.makeText(SwipeRecycleActivity.this, "编辑第" + adapterPosition + "个条目", Toast.LENGTH_LONG).show();
                    }
                    else
                    {
                        Toast.makeText(SwipeRecycleActivity.this, "删除第" + adapterPosition + "个条目", Toast.LENGTH_LONG).show();
                        list.remove(adapterPosition);
                        listAdapter.notifyDataSetChanged();
                    }
                }
            }
        };
    }
    private void initdata() {
        for (int i = 0; i < 30; i++) {
            list.add("添加-=-=-=-=-=-=-=-=数据" + i);
        }
    }
}

==================================华丽的分割线=======================================

布局代码

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    tools:context="com.wdcloud.selectfiletest.SwipeRecycleActivity">
    <com.yanzhenjie.recyclerview.swipe.SwipeMenuRecyclerView
        android:id="@+id/rv_swipe"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </com.yanzhenjie.recyclerview.swipe.SwipeMenuRecyclerView>
</android.support.constraint.ConstraintLayout>
展开阅读全文

没有更多推荐了,返回首页