Android: create list with the recycler view

Before starting to show how we can make a list with the recycler view we go to answer to the mother of the question: why should we do this? How can this thing help us? The answer is very simple: because of personalization. In this way we can easily use a layout made by us, define different action for every object inside this layout, and the inflate all in a recycler view and make them appear like a list. Let’s show how!

First of all create a new xml layout resource (card_example.xml here) where define how the list row will appear. I write a very simple example that will appear very similar to one of the list view default style, but you can personalize all you want.

As you can see I realized some small arrangement, like set the padding, make the checkbox not clickable (we will see why). At the same way we can define what we like in the layout.

After that we have to create an adapter (StringArrayAdapter.java) that will process our data. In this example our data are a simple array of string but you can process all what you want, useful to set a complex row layout.

In a few words what we made is to get the list of strings that are our data, create a new StringHolder (based on ViewHolder) for any row inside the predefined onCreateViewHolder. Inside the StringHolder constructor we set all the object previous defined in the xml layout and the, finally, we manage the row and make it to do some work inside the onBindViewHolder method. In this example we set the text of our textViewString based on the stringList and set a click listener on all the view where, at the action, change the status of the checkbox.

Now we have almost done! In our activity (or fragment, practically where you have your RecyclerView) simply set the RecyclerView as you prefer and set it the above adapter.

To being more clear check the example. We create the our data to give to the adapter, here by a simple for, then create a new StringArrayAdapter object and, after we have initialized the RecyclerView, we set it the adapter. By this simple action the recycler will call all the adapter methods previously seen and we have done all.

Before I set the checkbox not clickable in the xml, the why is simple. I need this to be sure that the only click action that can change the checkbox status is made by the parent view, as written in the adapter class. Denying the click to the checkbox is the best way to make it looks like enabled and, meantime, avoid any possible “double” action, one from the our layout click listener and the other one from the checkbox.

Two more trick

The ListView, by default, implements a click listener interface, where retrieve the row click to made some actions. The RecyclerView does not have this possibility, but we could achieve it by the adapter in a very simple way.

First of all we have to create a new interface in the adapter class, in our example inside StringArrayAdapter.

Then we add a private onClickListener object to the main adapter class and the set method.

At the end just call the onClick method from the interface where we have already set an OnClickListener for the row view.

Now we just need to call the setOnClickListener method of our adapter where we need to retrieve the click and manage it.

 

The second trick is to add a divider between the rows. To made this we need to create a class that extends the RecyclerView.ItemDecoration and make it able to draw the divider. I paste an example of this.

Then the only thing that you have to do is to add the above DividerItemsRecyclerView to your RecyclerView.

That’s all, for any question just post a comment!

Share
Questo articolo è stato postato in Android