0

I am creating a launcher app and I am using RecyclerView and ItemTouchHelper for reorder icons and it works well.

But I want to drag and drop my items into another recycler view (folders, bottom icons, other pages)

Is it possible with ItemTouchHelper? how can I do that?

Here is my code:

val itemTouchHelper = ItemTouchHelper(object : ItemTouchHelper.Callback() {
    override fun getMovementFlags(
        recyclerView: RecyclerView,
        viewHolder: RecyclerView.ViewHolder
    ): Int {
        return makeMovementFlags(
            ItemTouchHelper.UP or ItemTouchHelper.DOWN or ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT,
            0
        )
    }

    override fun onMove(
        recyclerView: RecyclerView,
        viewHolder: RecyclerView.ViewHolder,
        target: RecyclerView.ViewHolder
    ): Boolean {
        adapter.moveItems(viewHolder.adapterPosition, target.adapterPosition)
        return true
    }


    override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
    }

    override fun isLongPressDragEnabled(): Boolean {
        return true
    }

    override fun onSelectedChanged(viewHolder: RecyclerView.ViewHolder?, actionState: Int) {
        super.onSelectedChanged(viewHolder, actionState)
        viewHolder?.itemView?.animate()?.setDuration(200)?.scaleX(1.09f)?.scaleY(1.09f)?.start()
    }

    override fun clearView(
        recyclerView: RecyclerView,
        viewHolder: RecyclerView.ViewHolder
    ) {
        super.clearView(recyclerView, viewHolder)
        viewHolder?.itemView?.animate()?.setDuration(200)?.scaleX(1f)?.scaleY(1f)?.start()
    }
})
itemTouchHelper.attachToRecyclerView(binding.recycler)

And the result:

myAppResult

João Dias
  • 16,277
  • 6
  • 33
  • 45
amir_a14
  • 1,478
  • 10
  • 15

1 Answers1

0

I wouldn't try to transfer a ViewHolder between two Adapters, instead I would use one Adapter with different view types, like the bottom row in the example looks different and has some special logic but I could see them belonging to the same Adapter.

When the ItemTouchHelper "picks" up the ViewHolder you can determine what it does when it lands on a specific kind of ViewHolder.

If it is dropped on top of a FolderViewHolder instead of shifting the other ViewHolders it would "place" the held ViewHolder inside of the FolderViewHolder, not the actual ViewHolder but its underlying data model.

The FolderViewHolder could contain a List of all of the data models that where dropped into it. At that point when the user clicks the FolderViewHolder you could use the same RecyclerView/Adapter/ViewHolder combination to display another grid view with the containing data.

avalerio
  • 2,072
  • 1
  • 12
  • 11
  • and how to move items between pages? two viewpager pages cannot use same recyclerview – amir_a14 Aug 31 '21 at 04:28
  • You will need to detect if the user is pulling the item off the screen, the `RecyclerView` should be able to tell you that info. When its pulled off the item's data needs to be passed to the new page and added to it's `RecyclerView`'s `Adapter`. How its done by the Android OS is most likely akin to trickery over an actual logical solution. Animations, Transitions, and view swapping. I mean if you look at the way `RecyclerView`s work they are filled with trickery. – avalerio Aug 31 '21 at 12:44
  • That's not good idea, I'm looking for a better way – amir_a14 Sep 04 '21 at 07:29