6

I have a RecyclerView and I want to show that RecyclerView with two sections. First time section one will be empty and I want to drag item from section two to section one. When item is added to section one and it should be deleted from section two and vice versa.

Please help me friends.RecyclerView

Vishal
  • 108
  • 1
  • 9

3 Answers3

3

Try this,it worked with me in my app,

In your adapter class put this below code,

here MessageList is name of the Arraylist,

public void swap(int firstPosition, int secondPosition)
{
    Collections.swap(MessageList, firstPosition, secondPosition);
    notifyItemMoved(firstPosition, secondPosition);
}  

now add this small class separately,

here Adapter is name of the Adapter class put your adapter name

public class MovieTouchHelper extends ItemTouchHelper.SimpleCallback {
    Aadapter recycleAdapter;

    public MovieTouchHelper(Aadapter recycleAdapter) {
        super(ItemTouchHelper.UP | ItemTouchHelper.DOWN, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT);
        this.recycleAdapter = recycleAdapter;
    }

    @Override
    public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
        recycleAdapter.swap(viewHolder.getAdapterPosition(), target.getAdapterPosition());
        return true;
    }

    @Override
    public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
        recycleAdapter.remove(viewHolder.getAdapterPosition());
    }
}

Then in your mainActivity where you defined recycleview,

ItemTouchHelper.Callback callback = new MovieTouchHelper(adapter);
ItemTouchHelper helper = new ItemTouchHelper(callback);
helper.attachToRecyclerView(rv_list);

here rv_list is name of recycleview.

Follow this steps and if you find any problem or you can't swip items then directly tell me...

See this GIF

enter image description here

bhumika rijiya
  • 440
  • 1
  • 5
  • 42
  • @Vishal you can also use this solution as you have the same problem regarding this .....:-):-) – bhumika rijiya Jul 23 '16 at 04:12
  • hello Bhumika, how can i categorize in group too? – Vishal Jul 23 '16 at 07:26
  • i want to added in two group first one is added and second one is invite just like my above picture – Vishal Jul 26 '16 at 04:57
  • @Vishal can i know wht's problem? – bhumika rijiya Jul 29 '16 at 06:20
  • i want click not touch just click on add button data move to top .BTW move not working properly sometime . – Vishal Jul 29 '16 at 06:46
  • Hi @bhumikarijiya i have done with drag n drop.but now i want to steak first sequence value. in above gif while you move 4th to 3rd then item should move but first textbox value should not reset, it should be as it is. any idea? – RaRa Sep 07 '19 at 19:59
0

You can use the following links:

https://github.com/cymcsg/UltimateRecyclerView

https://github.com/wasabeef/recyclerview-animators

use any one of this. I hope it will help you.

Import the lib in your project.

rak
  • 108
  • 1
  • 12
0

You should use the ItemTouchHelper from the RecyclerView.ItemDecoration class. I just accomplish the behavior you're willing by following this tutorial: https://medium.com/@ipaulpro/drag-and-swipe-with-recyclerview-b9456d2b1aaf#.p6vykzstq

Bruno Andrade
  • 271
  • 3
  • 6