‫In this video.
‫We will continue with the home fragment and set up the recycler view with a staggered layout manager.
‫But first, in order to solve this, I will create inside the main activity.
‫I will create a variable late in Var.
‫Note.
‫View model.
‫It's of type note view model.
‫Okay.
‫And I will make it public.
‫So you can see note view model.
‫It will not refer to this variable.
‫It refers to the.
‫Main activity.
‫Variable.
‫Now set up the recycler view in order to display the the nodes in a staggered way in a staggered layout.
‫We need to create a layout manager that uses the staggered grid layout and we design and set how many
‫grids, how many columns, how many items that we need to display using the staggered grid.
‫So note adapter equal to node adapter.
‫I'm creating an instance from this and binding.
‫Dot recycler view dot apply.
‫I'm using apply in order to get rid of binding.
‫Every time.
‫I don't need to use binding binding, so I will use layout manager equals staggered grid layout manager.
‫This staggered layout manager took into the first parameter the number of grids, because I need to
‫display two grids, two columns and the staggered grid layout manager dot.
‫The orientation, which is the vertical set, has fixed size to true and adapter equal to note adapter.
‫I'm setting the adapter for the recyclerview.
‫Okay, now activity.
‫Dot.
‫Let.
‫Inside this.
‫I need to use nodes.
‫View model dot get all nodes dot observe.
‫This is the observed that we've seen before using the lifecycle owner and I will pass the view lifecycle
‫owner.
‫The second parameter is the.
‫Note, here is the.
‫Lambda expression note adapter dot defer.
‫Dot submit list and I will pass the note here and I need to update the UI.
‫I will call this method here.
‫Or I will create it.
‫Alt+ enter to create this update UI and it took into the parameter a list of nodes from the modal class
‫that we've created before.
‫Now updating the UI.
‫I'm checking if there is no note, is not empty, is not empty here then binding.
‫Add a new cardview for the new node that we create.
‫Okay.
‫Binding dot cardview dot visibility equal to view dot gone.
‫And I need to use the binding here.
‫Dot recycler view dot visibility equal to view dot visible.
‫Else.
‫Here I am checking the recycler view.
‫I'm making it visible and removing the card view which will be displayed like this.
‫No nodes available.
‫So if there is any node, any new node that we need to add and it's correct without no null values inside
‫it, we can create a new node.
‫So I remove this card view and inside this recycler view I will be displaying the new node.
‫Okay.
‫This binding here dot card view.
‫Dot visibility equal to view dot visible and binding dot recycler view dot visibility equal to.
‫view.gov.
‫If there is any error in adding the note, then the Cardview will be displaying no nodes available and
‫the recyclerview will be not displaying the correct or the newly added.
‫Note.
‫Okay.
‫Now.
‫I will scroll to here.
‫I will add a new function or override a new function.
‫Oncreate.
‫Options menu.
‫So this is a this is a function used to display the menus and inflate the menus inside the fragments
‫or the activities.
‫So menu dot clear.
‫I'm clearing the menu inflator I need to inflate and display the menu for the home.
‫So I need to use inflator dot inflate dot r dot menu dot home menu.
‫This is the first menu we've created before.
‫And I will get from the menu search.
‫Equal to menu dot find item and I will search for id called menu search.
‫I am searching for the box that contains the magnifier here.
‫So if I go to the menu home menu and we can see this magnifier here, if the user click on this.
‫A magnifier.
‫We need to make a search query.
‫So our ID dot.
‫Action of you as search view.
‫So I'm customizing it as a search view.
‫So m menu search is submit is submit button enabled equal to false and menu search which is a search
‫view.
‫I'm customizing it as a search view set on query text listener query text listener to this.
‫So because that we've implement this interface search view dot query listener.
‫Okay.
‫This is how we use the search view in the Android studio and in the search and in the menus here.
‫Okay.
‫Now I will overwrite functions on query change, text submit and text change and the search node.
‫And if I scroll up, I can see that there is an error here.
‫If I hover it alt+ enter to implement the members, I need to implement these two members on query text
‫submit and on query text change.
‫Okay.
‫On quarry here.
‫I need to search.
‫Note I need to create a new function called search node, and otherwise it will return false.
‫Okay.
‫Here if.
‫New text, which is the string does not equal to null.
‫Then what to make?
‫We need to search.
‫The the notes for new text.
‫And here return.
‫True.
‫Now I will create this function.
‫So private function search node.
‫It took into the parameter query of type string.
‫And here I need to search for val search query equal to.
‫This.
‫Query.
‫Variable because I need to pass it to the SQL queries.
‫If I go back to the Dow.
‫Here inside the database, we can see that the query I need to pass the query as a string.
‫And I put it like this.
‫Like what?
‫Like the query.
‫And here.
‫Like what?
‫Like the query that I passed here, which is a string.
‫Okay, so please pay attention to this trick.
‫Nodes View model dot search node.
‫I'm calling the search node from the view model.
‫So if I open the view model here, I can see that the search view or search node method is called and
‫I need to pass a query.
‫So search query.
‫It's past, Dot.
‫Observe.
‫Observe Any change?
‫Because I am using the live data and the life cycle owner.
‫The list create a list which is a lambda expression note adapter dot defer.
‫I'm using the defer dot submit list from the from the diff util function a utility class and submit
‫list which is a function.
‫I'm passing the list inside it.
‫Okay.
‫I think there is a complex complex things and concepts.
‫But don't worry, you will be familiar with them if you practice them because they are templates, they
‫act as templates.
‫So think about mvvm as template.
‫You will always make the same code.
‫Okay.
‫So don't worry on destroy.
‫I need to stop this, bind this binding and I will make binding equal to null.
‫Okay.
‫This search note, I will comment it like.
‫At first.
‫Later on, we will see why.
‫Okay.
‫Only.
‫Note only this null.