RecylerView implements long-pressing, dragging, and changing the position of the item, recylerviewitem

Source: Internet
Author: User

RecylerView implements long-pressing, dragging, and changing the position of the item, recylerviewitem

:

Steps:

1. Create a new class inheritance? ItemTouchHelper. Callback:

Class ItemDrag extends ItemTouchHelper. callback {// set the movable flag. I use GridLayout. All movable flags are up, down, left, right. if it is LinerLayout, It is up, down. @ Override public int getMovementFlags (RecyclerView recyclerView, RecyclerView. viewHolder viewHolder) {int dragFlags = ItemTouchHelper. UP | ItemTouchHelper. DOWN | ItemTouchHelper. LEFT | ItemTouchHelper. RIGHT; int swipeFlags = 0; return makeMovementFlags (dragFlags, swipeFlags );} // This method @ Override public boolean onMove (RecyclerView recyclerView, RecyclerView. viewHolder viewHolder, RecyclerView. viewHolder target) {int fromPosition = viewHolder. getAdapterPosition (); // obtain the position int toPosition = target of the first item. getAdapterPosition (); // obtain the position Collections of the target item. swap (mChoosed, fromPosition, toPosition); // mChoosed is the data set of Recylerview. Two items are exchanged for mChoosedTagAdapter. yi TemMoved (fromPosition, toPosition); // return true for the recylerview adapter notification exchange update;} @ Override public boolean isLongPressDragEnabled () {// you can set whether to enable long-press to drag mShowIcon = true; mTagAdapter. notifyDataSetChanged (); mChoosedTagAdapter. notifyDataSetChanged (); mButton. setText ("finished"); return true ;}@ Override // callback public void onSwiped (RecyclerView. viewHolder viewHolder, int direction) {}@ Override // sets the background color pub when moving Lic void onSelectedChanged (RecyclerView. ViewHolder viewHolder, int actionState) {if (actionState! = ItemTouchHelper. ACTION_STATE_IDLE) {viewHolder. itemView. setBackgroundColor (Color. LTGRAY);} super. onSelectedChanged (viewHolder, actionState) ;}@ Override // restore the background color public void clearView (RecyclerView recyclerView, RecyclerView. viewHolder viewHolder) {super. clearView (recyclerView, viewHolder); viewHolder. itemView. setBackgroundColor (0 );}}

2. CreateItemTouchHelper class instance, Parameters are written for youThe class that inherits callbackAndRecylerview sets this ItemTouchHelper.

Then OK.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.