ItemTouchHelper profile

ItemTouchHelper is a RecyclerView sidebar delete and drag move utility class. We can by integrating ItemTouchHelper. The Callback or ItemTouchHelper. SimpleCallback abstract class and implement corresponding methods in order to realize the sideslip delete and drag and drop the function of the mobile.

ItemTouchHelper. Introduction of Callback

ItemTouchHelper. Callback and ItemTouchHelper. SimpleCallback are abstract classes, and SimpleCallback inheritance and Callback. So let’s take a look at some of the common methods in Callback and what they do.

isItemViewSwipeEnabled()
public boolean isItemViewSwipeEnabled() {
    return true;
}
Copy the code

IsItemViewSwipeEnabled () returns the value that determines whether ItemTouchHelper responds to the user’s swipe delete gesture. The default value is true.

isLongPressDragEnabled()
public boolean isLongPressDragEnabled() {
    return true;
}
Copy the code

IsLongPressDragEnabled () returns the value that determines whether ItemTouchHelper responds to the user’s drag-and-hold gesture. By default, it returns true.

getMovementFlags
public abstract int getMovementFlags(RecyclerView recyclerView, ViewHolder viewHolder);
Copy the code

GetMovementFlags () is the abstract method we must implement, and the return value of this method is a composite identifier that defines the sliding direction of SWIP and drag.

Normally we generate this flag by either makeMovementFlag(int, int) or makeFlag(int, int).

MakeMovementFlag, makeFlag
@param swipeFlags public static int makeMovementFlags(int dragFlags, int swipeFlags) { return makeFlag(); } public static int makeFlag(int dragFlags, int swipeFlags) { }Copy the code

ItemTouchHelper provides us with the following constants:

  • ItemTouchHelper.TopUp:
  • ItemTouchHelper.DOWNDown:
  • ItemTouchHelper.LEFTLeft:
  • ItemTouchHelper.RIGHTTo the right:
  • ItemTouchHelper.START: Horizontal starting direction, depending onRecyclerViewThe layout direction of.
  • ItemTouchHelper.END: horizontal end direction, depending onRecyclerViewThe layout direction of.

These constants can be used as parameters to the makeMovementFlag(int, int) method and can be used in combination. For example, if RecylerView supports dragging up and down to replace a position and right swiping to delete, it would look like this:

makeMovementFlags(ItemTouchHelper.UP | ItemTouchHelper.DOWN, ItemTouchHelper.RIGHT );
Copy the code
onMove
/** * @param viewHolder Drag Item * @param target change position object */ public abstract Boolean onMove(RecyclerView recyclerView, ViewHolder viewHolder, ViewHolder target);Copy the code

OnMove () is called when ItemTouchHeler wants the item represented by viewHolder to switch places with the item represented by the targe argument. If true is returned, the two switch places, otherwise they cannot switch places.

Note: When implementing the onMove method, note the following:

  1. Two adjacent at a time during the dragItemSwap positions betweenonMoveMethods.
  2. whenonMovereturntrueIs callednotifyItemMoved()Methods swap places and data sources swap accordingly.
onSwiped
public abstract void onSwiped(ViewHolder viewHolder, int direction);
Copy the code

The onSwiped() method is called back when the user swipes to delete an item.

Note: When the onSwiped() method is called, the notifyItemRemoved() method is called to remove the item and the data source is changed accordingly.

onChildDraw
public void onChildDraw(Canvas c, RecyclerView recyclerView, ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {}
Copy the code

OnChildDraw () is called in recylerView.ondraw () whenever the RecyclerView changes due to drag and slide.

You can override this method if you need to customize how the view interacts with the user.

onDrawOver

OnDrawOver is similar to onChildDraw except that it is drawn on top of the Item View layer.

onSelectedChanged
Public void onSelectedChanged(ViewHolder ViewHolder, int actionState) {}Copy the code

This method is called when the ViewHolder is dragged, sideslipped, or released. ActionState can be the following values:

  • ItemTouchHelper.ACTION_STATE_IDLEReleased:
  • ItemTouchHelper.ACTION_STATE_SWIPE: by drag and drop
  • ItemTouchHelper.ACTION_STATE_DRAG: the sideslip
clearView
public void clearView(RecyclerView recyclerView, ViewHolder viewHolder) {}
Copy the code

This method is called when the interaction between the user and the view ends and the animation completes. In this method, you can clean up actions made in the onSelectedChanged(), onChildDraw() methods, animations, and so on.

The basic use

MyItemTouchCallBack.kt

class MyItemTouchCallBack(private val onItemTouchListener: OnItemTouchListener) : ItemTouchHelper.Callback() { override fun isItemViewSwipeEnabled(): Boolean { return true } override fun isLongPressDragEnabled(): Boolean { return true } override fun getMovementFlags(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder): Int { return if (recyclerView.layoutManager is LinearLayoutManager) { makeMovementFlags(ItemTouchHelper.UP or ItemTouchHelper.DOWN, ItemTouchHelper.RIGHT) } else { makeMovementFlags(ItemTouchHelper.UP or ItemTouchHelper.DOWN or ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT, 0) } } override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {/ / here is the main code of sideslip delete val position. = viewHolder adapterPosition onItemTouchListener. OnSwiped (position)} override fun onMove(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder, target: RecyclerView.ViewHolder): Boolean { val fromPosition = viewHolder.adapterPosition val toPosition = target.adapterPosition onItemTouchListener.onMove(fromPosition, toPosition) return true } override fun onSelectedChanged(viewHolder: RecyclerView.ViewHolder? , actionState: Int) { super.onSelectedChanged(viewHolder, actionState) onItemTouchListener.onSelectedChanged(viewHolder, actionState) } override fun clearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) { super.clearView(recyclerView, ViewHolder) onItemTouchListener. OnClear (viewHolder)} / mobile exchanging data update to monitor * * * * / interface onItemTouchListener {/ * * * */ Fun onMove(fromPosition: Int, toPosition: Int) */ Fun onSwiped(position: Int) Int) /** * Call */ fun onSelectedChanged(viewHolder: recyclerView.viewholder? ActionState: Int) / / fun onClear(ViewHolder: recyclerView.viewholder)}}Copy the code

ItemTouchHelperActivity.kt

class ItemTouchHelperActivity: AppCompatActivity() { private lateinit var itemtouchAdapter: ItemTouchAdapter private Val itemList = arrayListOf(" Monday ", "Tuesday "," Wednesday ", "Thursday "," Friday ", "Saturday ", Override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_recyclerview_itemtouchhelper) itemtouchAdapter = ItemTouchAdapter(this, itemList) recyclerView.layoutManager = LinearLayoutManager(this) recyclerView.adapter = itemtouchAdapter recyclerView.addItemDecoration(LayoutItemDecoration(8, true)) val touchCallBack = MyItemTouchCallBack(onItemTouchListener) val itemTouchHelper = ItemTouchHelper(touchCallBack)  itemTouchHelper.attachToRecyclerView(recyclerView) } private val onItemTouchListener = object : MyItemTouchCallBack.OnItemTouchListener { override fun onMove(fromPosition: Int, toPosition: Int) { itemtouchAdapter.notifyItemMoved(fromPosition, toPosition) Collections.swap(itemList, fromPosition, toPosition) } override fun onSelectedChanged(viewHolder: RecyclerView.ViewHolder? , actionState: Int) { if (actionState == ItemTouchHelper.ACTION_STATE_DRAG) { runViewAnimation(viewHolder!! .itemView, true) } } override fun onSwiped(position: Int) { itemtouchAdapter.notifyItemRemoved(position) itemList.removeAt(position) } override fun onClear(viewHolder: RecyclerView.ViewHolder) { runViewAnimation(viewHolder.itemView, False)}} / private fun runViewAnimation(view: view? , isSelected: Boolean) { if (null ! = view) { val xScaleAnimator = ObjectAnimator.ofFloat(view, "scaleX", If (isSelected) 1.04f else 1.0f) val yScaleAnimator = objectAnimator.offloat (view, "scaleY", If (isSelected) 1.04f else 1.0f) val animatorSet = animatorSet () animatorset.duration = 200 animatorSet.play(xScaleAnimator).with(yScaleAnimator) animatorSet.start() } } } class ItemTouchAdapter(val context: Context, private val itemList: List<String>) : RecyclerView.Adapter<ItemTouchViewHolder>() { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemTouchViewHolder { return ItemTouchViewHolder(LayoutInflater.from(context).inflate(R.layout.view_item, parent, false)) } override fun getItemCount(): Int { return itemList.size } override fun onBindViewHolder(holder: ItemTouchViewHolder, position: Int) { holder.tvItem.text = itemList[position] } } class ItemTouchViewHolder(view: View) : RecyclerView.ViewHolder(view) { val tvItem: TextView = view.findViewById(R.id.tvItem) }Copy the code