Android | RecyclerView Right Swipe Delete in Fragment in Kotlin

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome back to my channel in today's video i'm going to show you how you can use recycler view on a fragment and then tapping on a item on the recycler with an opening a new fragment or for the details also i'm going to show you how you can swipe right and then delete an item so let's begin with adding a layout in our main activity and in this video i'm going to use the cortland instead of java this is our linear layout and here we are going to add a fragment on this layout so for that we need to create a fact fragment so i'm creating a empty fragment and i'm going to name the main and i'm going to use the caught line so i'm deleting all the unnecessary command and the code which is not required we don't need any parameters which is going to be passed from the activity to fragment so i'm deleting all these since it already have two parameters so let me remove all those two parameter two so now it will create fragment and just to use the fragment we need the transactions fragment transaction and on the fragment transaction we can call replace function and then we will use the container which we added in our layout and then we'll pass the fragment also i am going to pass a tag for this fragment i'll show you why i'm passing this tag because i'm going to use it when uh item will be clicked on the recycler view so in the fragment main we are going to use the recycler view so i'm simply changing the texture to recycler view and giving it as id and we don't need this text so i'm removing it now inside the main fragment we need to grab the recycler view and we need to initialize and set our layout manager so let's do that for that i will create a separate function with the name init recyclerview and we'll pass the same id what we gave in the layout that is the recycler view and then we'll set the linear layout manager now you need to set the adapter but we don't have any adapter so let's create that adapter but that adapter need to have some kind of a data so let's start with our data class so this data model class is going to have one string type and i'm giving his variable name as the title of string now uh we need to create a class for our adapter and this adapter is going to hold the data released of the data model type now we will inflate our layout so we don't have our layout so let's create a layout as well and inside this layout i'm just going to keep uh one text view this this is just because we have in our data class as a one parameter that is tighter and i'm changing it to constraint layout instead of linear layout we'll pass that layout file name here parent comma false that will return as the view now as i said uh we need to have the data to display in the recycler view so we'll pass the data from the main fragment so we'll hold it inside the constructor itself that is going to be the list of data model type uh you now we need to as i said like we need to handle the recycler item click so for that i need to create a interface and i'm declaring a function on item click and which is going to hold the data model type of data and that listener also we are going to take in inside the constructor and that both the parameter both the values we are going to pass it from the main fragment and here we are going to handle its item click item view click and we'll pass the data model object so our adapter is ready now let's go back to our main fragment and set it to our recycler view you and we'll implement this click listen listener on our main fragment we'll override that function there you go now we need to pass this less data we'll add some data later and we'll set this adapter to our recycler view now list data is currently empty so let's create a separate function to build some data in this video i'm not going to show you like the api call or anything it's just simply using the static data so list data is currently okay so it's a list we need to have that array list to call the add function and i'm going to add some static data inside this as a string and we'll call this build data function so this is going to be the detailed fragment same as kotlin and we don't need so many parameters i'm removing all the parameter or the extra code since we have just title in our data model data class so i'm just going to pass one parameter so i'm just keeping one and removing others and it already have a text view i'm just giving it a id setting the text size also i'm going to use the constraint layout in this as well and setting this text to constraints so that it can just display that name which you pass from the main fragment in the center of the screen and we'll simply set that param1 which we pass inside the new instance function from the main fragment now here we'll attach the main detail fragment to the activity now so here i'm going to use the tag which i set and the main activity and pass the value the title same as we need the transaction since we already have a one fragment over the activity attached so i'm going to hide that fragment and then i'm going to add this detailed fragment over it so just we need to find that fragment which is already there we need to use this tag value and then we'll find and then we'll hide it and now this transaction manager there will add the detail fragment and pass the detail fragment and we don't need a drag id here if you want you can pass it and then we'll add this backstack null just to handle the back button click and then we'll commit it so this is how we are going to handle its item click let's run and see how it look like there you go so you see the recycler view and we can simply see the next fragment detail fragment that is showing the data that's fantastic now the next part of the video is we need to handle the right swipe and delete item from the recycler view so for that we need item touch helper dot simple callback so we need to implement this just to handle the swipe right and once user will swipe right i'm not going to simply delete it and we'll ask user in either they want to delete or cancel that deletion so for that i am going to show you a dialog before deletion i'm going to call a show dialog whenever item will be deleted or item will be swiped right let me create a dialog alloy dialog here before that we need to attach that item touch and we'll attach our recycler view here now i am creating a dialog whenever user is swiping right at that time instead of directly deleting i'm going to show a dialog ask user if for the confirmation if they want to delete then they can confirm and then item will be delete if user press cancel then item will not delete it will come back you so here with the help of view holder we can get the position of the item where user is swiping right so based in this position we just simply need to notify our adapter so in case of cancel we don't need to perform anything with simply call notified data set change on that particular item in case of confirm we need to remove that item from our list data and then we need to call notify item data remove removed and then set the position it's simple and it's easy let's run and see so you can see the data as tapping is working fine now swiping right and see the dialog if cancel then item is coming back if i'm confirmed then it deleted perfect so it's confirmed deleted so guys that's all for today's video thank you so much for watching if you have any question you can ask me in comment section thank you
Info
Channel: LearningWorldz
Views: 2,927
Rating: 5 out of 5
Keywords:
Id: 5JsLk8IkhXE
Channel Id: undefined
Length: 23min 16sec (1396 seconds)
Published: Fri Feb 26 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.