Welcome back and this video, we are going to implement a drag and drop feature, as you can see here,
I can click on one of those elements and hold it and then I can drag it so we can now drag carts here.
As you can see, it will also update the database here once we do so, even though we have way too many
progress dialogue.
So we'll definitely have to take care of that at one point.
But overall, you can see that the drag and drop feature works and this is also stored in the database.
So if I go back to my third board, you can see that it is still there as we expected it.
Now, the thing is that I will not implement the functionality in the videos, at least where you can
drag from one list to another.
So let's say we have another list here, too.
So I'm going to call it and let me.
Add a card here, card to one, for example, and then I cannot drag from one list to another, so that
will not work, even though there are ways to implement that.
And I can even show you some code for that.
But, um.
Code is too complex or too much, basically, for these videos, I think, because the product is already
big enough.
So if you really need that feature, check out the projector at the end of the course.
But for now, let's get started with this so it all happens on this task list activity, but the actual
magic happens in the adapter for it, so in the task list adapter.
All right.
So let's go over to our adapters here.
Task list item adapter and in here.
Inside of the on Pind view, holder at the very bottom, still inside the safe clothes, but at the
very bottom underneath our own click listener here.
That's where we can start the implementation.
So while divider item declaration.
Which will be a divider item, declaration object.
In which I need to pass in the context, so I'm going to pass in the context.
As well as.
The settings for which will be.
The fighter item decoration, not vertical.
OK, so you have multiple decoration options here, vertical or horizontal.
I'm going to use vertical here so that I can drag from top to bottom or up and down, so to speak.
OK, so then we can now use this for our holder item view our V card list and here we add the item decoration
that we just prepared.
So the divider item decoration to it.
Then we need an item, touch helper, so whenever you want to use the drag and drop stuff, the item
touch helper is your friend.
So Vul helper is going to be the item touch helper object.
And here we need to create an object of the item to touch helper simple callback.
So let's import that.
And here.
Let's use item, touch, helper, dot, simple callback, so let's create an object of that, by the
way, we need to add the object keyword here, simple callback.
And the simple call back needs to know which direction you want to use.
I'm going to use item, touch, helper, dot up or item torch helper dot down.
And then.
The count or swept the years, I'm going to set to zero.
OK, so this is just the.
Either of my simple callback object, now I need to implement some functions in there and to be precise,
the on move function.
So let's implement the members here on move and on swept aside those to say they will be automatically
generated for us in here.
So here we can now implement the functionality that should happen once we move.
And I'm going to get the Drax position.
So I'll direct position and then direct.
Adapter.
Position, I guess by doing that, we can get it from the viewer holder here.
Well, actually.
It's called fueler.
So I'm going to call it direct, so it's a little more understandable.
So here this is where directed from or what the adapter position of that is.
And then.
I want to know the target position, so where does it come from and where does it go basically?
So here, target dot adapter position and at that point I will need a global variable which will store
the track position.
OK, so here, let's go ahead at the very top of our task list items, Ed, I need to create a global
variable for dragging.
So here private var m position dragged from which I'm going to assign minus one to you and then private
var m position direct to which I'm going to assign minus one as well.
OK, so if we do that, we definitely make sure that we have them at negative position so to speak.
So we know that it's the default statement or value that we assign that here.
All right, and now we can then check if the direct or indirect from is minus one.
Then we want to set them position direct from to the direct position, so whatever the adapter position
of the direct value is the recycler view view holder.
That should be the one.
And then we should also set and position direct to.
As the target position.
So whatever the target position is that we get from the on move function, so to speak, is the one
that we want to set for this global variable here.
All right, next.
We need to collections.
So here collections, this class here, and it has a function which is called swap, so here we can
swap list elements.
So list at the position.
Thought Cartes.
To be direct position.
And then we also need to pass into target position.
OK, so we just say, OK, just reposition to carts here or the position of the carts object, so here
inside of the array list changed the position of it.
OK, so that's what the swap function does, it just swaps those two, all right.
And the next thing is that we need to notify our adapter that were that the word changes made.
Right.
Otherwise, it will not know and it will not really change anything.
OK, notify item moved is the function we can call here and then here we need to pass and the direct
position.
To the target position.
OK, so those two objects were moved, now our adapter knows about it as well, and the rest of you
will display that accordingly.
Of course, we still get an error here because our on move function requires a boolean and we can just
return false here.
So return true of moved and falls otherwise.
So that's what we do here.
All right, and the task is the activity itself, we will also need a function which will take care
of moving things around.
So let's create one here, which will be our fun update cards and task list.
So task list position.
Should be an integer and then.
The cards that we want to change, so cards that hurray or call an array list of cards.
So these are the details that we want to have if this update is about to be called or should be called.
So here, because we have cards that are not really cards, so if we look at it, we have this at card
text here, which is not really a card, but it's still card of the greatest.
So we need to get rid of that by using this code here.
And then we can set the import task list.
But or at the task list position that Cartes.
To be the new cards that were passed to us.
And of course, it's not The Selecter.
And then we can show the progress dialogue.
With the police wait statements, so we need to use the resources once again and then we want to update
it and that is what we have done multiple times already.
So this firestorm.
Update task and update the task list with the latest and board details.
All right, and then there's one last thing to do, and that is not the on swipe to function.
So we don't want to throw an arrow if there is nothing happening in the on swipe.
So we can leave it at that.
But we need to implement another function.
So we need to override the clear view function so that one will be called once the drag and again dropping
is over.
So once we're done with that.
So what I want to do here is I want to check if the M.
Position direct from is not equal minus one and.
The imposition direct two is not equal.
Minus one.
And also the.
Direct position from is not equal the direct position to OK, so let's add this as well.
Is not equal.
To this one here.
So all of that is true, then I want to call this up, that card list and TASC function.
So what I can do here is I can.
Now run context.
As task list activity, that update in cards and task list here, we need to pass into position and
then to.
The list of cards so list.
Position.
But Cartes.
And finally.
We reset the imposition from so, by the way, to make this a little more readable, I think it makes
sense to add the square brackets here or the curly brackets.
So then it will be a little bit more readable.
OK, so then we can reset the values here to be minus one for the from value and position from our direct
to you should also be reset to minus one.
OK, so at this point, we can test our application because as you can see, we are updating stuff here
with updated cards and task list.
Now, it's not great out anymore.
It's yellow, which means that it is called and it's called here.
And then there is one less thing to do, and that is to use this item touch helper, because we prepared
everything for it, but we are never actually using it.
So at the very bottom of this whole helper thing, and that is just after this closing.
Record, that's where we can then use help or not attach to reset of U.
Holder, the item you thought, Harvey.
Cardless.
OK, and at this point, our respective you should have this.
Pretty cool drag and drop feature, so let's test it on our device.
That we are bought with image.
So we have the list with three elements here, let's click on card three and drag it around.
And as you can see.
The cards drag around, so that seems to work totally fine.
OK, so that is a drag and drop feature, I hope you like it, of course, is not as cool as dragging
and dropping from one.
Cards or lists to another.
So dragging a card from one place to another, but as I said, if you really need that, check out the
super long code that you will need for that in the link attached.
All right.
So see you in the next video.