Welcome back.
Having those buttons in the RecyclerView looks untidy,
and it doesn't fit with modern user interfaces.
Users are used to removing items by swiping them left.
That's not very hard to implement,
and we'll see how to do that in the next few videos.
We could use a gesture detector,
and we've seen how those work in the FlickrBrowserApp.
But for this app, I'm going to use an ItemTouch helper.
We'll check for swipes in MainActivityFragment.
You set up an ItemTouchHelper
then attach it to your RecyclerView.
I'll do that in OnViewCreated.
That's where we configure the RecycleView.
So it makes sense to attach the ItemTouchHelper in there too.
Accept the import from
androidx.RecyclerView.widget.itemtouchhelper
if it's not imported automatically.
The ItemTouchHelper will replace the delete buttons
OnClickListener,
that's in our adapter and we'll have a look at that in a moment.
But first we've got an error.
When we call OnDelete click, on line 77,
we need to give it a task, but we don't know the task.
The adapter does or more precisely
the adapter's ViewHolder class does.
That's where we're getting the task from on line 76.
To fix the error, we need to make the ViewHoldersTask available.
That's one of the few changes we have to make in the adapter.
So let's have a look at our CursorRecyclerViewAdapter class.
We defined our ViewHolder at the start of the file
up on line 17.
It gets passed a task in its bind function,
and we need to make that available as a class attribute.
The ViewHolder won't be displayed in the RecyclerView until
after its bind function has been called,
that means we can use late init there
rather than using a nullable type.
Our task attribute will get its value when the bind function is called.
We're going to remove the delete button.
There's no point leaving it in the layout when we're going to be swiping.
That means we'll no longer be able to refer to it in our ViewHolder.
So I'll delete it.
There's also no need to set its OnClickListener and we can delete those lines.
If we're no longer setting the DeleteListener,
there's no need to have it in our interface. So I'll remove that as well.
The final change is in the OnBindViewHolder function
where we hide the delete button.
We don't need that line either.
That's all the changes to our adapter.
Close it, and return to MainActivityFragment.
We fixed the error, and now we've got a task
to pass to the OnDeleteClick function.
We've got another error but I'll come to that soon. So what's going on here?
This is boilerplate code.
When you want to attach an ItemTouchHelper to a RecyclerView,
this is the standard code to do it.
Our ItemTouchHelper contains an object of type
ItemTouchHelper.SimpleCallback.
SimpleCallback creates a callback for drag and swipe operations.
Callback is an abstract class and we have to provide implementations
for two functions,
OnMove and OnSwiped.
OnMove is used if you want to allow the recycler items to be dragged up and down.
OnSwiped is where you define the behavior you want
when the item's swiped,
and that's what we're doing here.
Our code is simple if the item swiped left,
which we check on line 75,
then we'll pass the ViewHolders task to OnDeleteClick.
That's our ItemTouchHelper written. But there's one more step we need to do,
we need to attach it to the RecyclerView.
Otherwise, it won't be used when the items are swiped.
Line 82 attaches our ItemTouchListener to the RecyclerView.
Before we can test it, we need to fix the error
in OnDeleteClicked
on line 89.
We've got an error because we removed this function from the interface.
It's no longer overriding anything, so we have to remove the override.
All right. The moment of truth.
Let's see if we can delete tasks by swiping them left.
Run the app, and swipe some tasks.
To swipe in the emulators, you need to click the object to swipe
then flick the mouse left.
Make sure you swipe all the way.
If you only sort of swipe halfway, the animation puts the item back in the list.
It's much easier to do on a physical device with your finger
rather than using a mouse in an emulator.
That's looking good. We've got our confirmation dialog.
I'll tap delete and the task's deleted.
That wasn't a lot of work to implement swiping.
We added a simple TouchHelper
and attached it to the RecyclerView.
And it almost works.
There's a problem.
To demonstrate it, I'll choose task 6.
Make a mental note of the task number and then swipe it.
When the dialog appears, tap cancel.
That looks worrying, but it's normal behavior.
The task hasn't been deleted from the database.
We can confirm that by refreshing the activity.
Rotating the device is an easy way to do that.
When the recycler redraws we get our task back.
As I said that's normal behavior.
When you swipe something an animation runs
and it gets removed from the RecycleView.
In the next video, we'll see how to reverse that
to get our task back if the user cancels the delete.
I'll see you there.