Copyright statement: reprint must indicate the article from YanZhenJie blog: blog.csdn.net/yanzhenjie1…

A few days ago I wrote a blog about RecyclerView sliding delete Item, RecyclerView long press drag Item, originally very simple to use, reading is quite high, the original blog portal.

Today, I will introduce a RecyclerView Item side-sliding menu. RecyclerView slides to delete Item, and RecyclerView drags and drags Item. The effect is shown in the following figure. You can also download demo APK specific operation experience under SwipeRecyclerView is powerful.

Today’s blog will solve these problems perfectly: 1. Specify that an Item can’t be swiped or dragged. 2. RecyclerView Item Add side slide menu. 3. Automatically open a specified Item menu. 4. Solve the sliding conflict between RecyclerView and Item. 5. Solve the conflict between Item sliding and drop-down refresh framework of RecyclerView.

SwipeRecyclerView’s source code is hosted at Github: github.com/yanzhenjie/… Welcome Star.

Demo APK download

rendering

If the page load speed is slow, you can download the APK of the demo to see the effect.

Lateral spreads menu

RecyclerView Item Sideslip menu. The left and right sides can be added to the side slide menu, menu Item includes icon, text, click background color, etc. We can also determine the appearance and type of the Menu of an Item according to its ViewType:

Drag and slide menu

RecyclerView Item Long press and drag Item to move position. At the same time, support Item long drag, Item side slide display menu. LinearLayoutManager, GridLayoutManager, LinearLayoutManager, LinearLayoutManager, LinearLayoutManager

Drag and slide to delete

You can specify that an Item cannot be dragged, or that an Item cannot be deleted by sliding:

Reference method

  • Eclipse download the source code.
  • AndroidStudio uses Gradle builds to add dependencies (recommended)
The compile 'com. Yanzhenjie: recyclerview - swipe: 1.0.0'Copy the code

The RecyclerView version referenced in this library is as follows:

The compile 'com. Android. Support: recyclerview - v7:23.4.0'Copy the code

Used to introduce

The key implementation is listed here, please refer to the demo for details, or add the communication group on the left side of my blog or the public wechat who follow me to discuss.

Realize the principle of

  1. The sidesrag menu is implemented using a custom Layout, inherited from FragmeLayout.
  2. Dragging and dragging an Item and sliding it to delete is implemented by ItemTouchHelper.

Enable SwipeReyclerView’s long-click Item drag function and side-swipe delete function

recyclerView.setLongPressDragEnabled(true); recyclerView.setItemViewSwipeEnabled(true); recyclerView.setOnItemMoveListener(onItemMoveListener); . /** * listen when Item moves. */ private OnItemMoveListener onItemMoveListener = new OnItemMoveListener() { @Override public boolean onItemMove(int fromPosition, int toPosition) { Collections.swap(mStrings, fromPosition, toPosition); mMenuAdapter.notifyItemMoved(fromPosition, toPosition); return true; } @Override public void onItemDismiss(int position) { mStrings.remove(position); mMenuAdapter.notifyItemRemoved(position); Toast.show(mContext, "clause" + position + "deleted. ); }};Copy the code

Add Item slider menu

The side menu can automatically open the menu of an Item, and can specify the left or right side:


recyclerView.openLeftMenu(0);


recyclerView.openRightMenu(0);Copy the code
  • First step, reference custom View: SwipeMenuReyclerView:
Copy the code
SwipeMenuRecyclerView swipeMenuRecyclerView = findViewById(R.id.recycler_view);

swipeMenuRecyclerView.setSwipeMenuCreator(swipeMenuCreator);

swipeMenuRecyclerView.setSwipeMenuItemClickListener(menuItemClickListener);Copy the code
/** * menu creator. Called when Item is about to create a menu. */ private SwipeMenuCreator swipeMenuCreator = new SwipeMenuCreator() { @Override public void onCreateMenu(SwipeMenu swipeLeftMenu, SwipeMenu swipeRightMenu, int viewType) { SwipeMenuItem addItem = new SwipeMenuItem(mContext) .setBackgroundDrawable(R.drawable.selector_green) .setImage(R.mipmap.ic_action_add) .setWidth(size) .setHeight(size); swipeLeftMenu.addMenuItem(addItem); SwipeMenuItem deleteItem = new SwipeMenuItem(mContext) .setBackgroundDrawable(R.drawable.selector_red) .setimage (r.map.ic_action_delete).settext (" Delete ").settextColor (color.white).settextSize (16).setwidth (size).setimage (r.map.ic_action_delete).settext (" delete ").settextColor (color.white).settextSize (16).setwidth (size) .setHeight(size); swipeRightMenu.addMenuItem(deleteItem); }};Copy the code
  • Step 4: Inherit SwipeMenuAdapter and use it as a normal Adapter:
public class MenuAdapter extends SwipeMenuAdapter {

    @Override
    public int getItemCount() {
        return 0;
    }

    @Override
    public View onCreateContentView(ViewGroup parent, int viewType) {
        return null;
    }

    @Override
    public ViewHolder onCompatCreateViewHolder(View realContentView, int viewType) {
        return null;
    }

    @Override
    public void onBindViewHolder(MenuAdapter.DefaultViewHolder holder, int position) {
    }
}Copy the code

That’s it. For more details, please refer to the Github demo and source code download portal.

Copyright statement: reprint must indicate the article from YanZhenJie blog: blog.csdn.net/yanzhenjie1…

Welcome to join my technical exchange group: Android Yugong Yishan ③ 547839514 welcome to pay attention to my public account, constantly push preferred blog posts, excellent open source projects, welcome to contribute: