Android Tutorial Basic difference between a Standard ListView and Amir_RecyclerView

During the introduction to Amir_RecyclerView (ARV), it was necessary to understand the basic relationships between data and display.

ListView is a view group that displays a list of scrollable items. The list items are automatically inserted to the list using an Adapter that pulls content from a source such as an array or database query and converts each item result into a view that's placed into the list.

If the data source has 100 entries, also 100 items with all views on them will be created.

In order to keep the performance of the displayed list bearable for complex layouts with graphics, you may have to use additional routines to realize e.g. a "LazyLoading".

Items_StandardListview.jpg


The RecyclerView is an advanced and flexible version of ListView.

In the RecyclerView model, several different components work together to display your data.

RV_LM_AD_DS.jpg


The visible part on the screen is the RecyclerView [C], which is added to the layout as a container that holds all the parts together. RecyclerView fills itself with views provided by a "Layout manager".​

The views in the list are represented by "Viewholder" objects.​

The RecyclerView creates only as many Viewholder as are needed to display the on-screen portion of the dynamic content, plus a few additional ones. As the user scrolls through the list, RecyclerView takes the off-screen views and links them back to the data scrolling on the screen.​
The Viewholder objects are managed using an "Adapter".

If required, the adapter creates a Viewholder. The adapter also binds the Viewholders to their data. This is done by assigning the Viewholder to a position and calling the onBindViewHolder() method of the adapter. This method uses the position of the view holder to determine what the content should be based on its list position.​

This RecyclerView model does a lot of optimization work so you don't have to.

When the list is filled for the first time, it creates and binds some viewholders on both sides of the list. For example, if the view displays list positions 0 through 9, RecyclerView creates and binds these viewholders and can also create and bind the viewholders for position 10. So when the user scrolls through the list, the next item is ready for display.

As the user scrolls through the list, the RecyclerView creates new Viewholder as needed. It also stores the view holders that are scrolled in the off-screen area so that they can be reused.

If the user changes the direction in which he scrolled, the view holders scrolled from the screen can be returned. If, on the other hand, the user scrolls in the same direction over and over, the view holders that have been the longest away from the screen can be reattached to new data.
If the data of the displayed elements changes, the adapter is simply notified by calling a corresponding method ...notify...(). The adapter's built-in code then merges the affected elements.​

Items_RecyclerView.jpg

Although the AVR looks pretty complicated at first glance, it is very easy to use for the B4A developer.

The decisive difference that needs to be understood for practical work is how the list is filled.​

In the standard list view, the data records are called one after the other in a loop and the content is added as a new list item.
B4X:
    For i = 1 To 300
        ListView1.AddSingleLine("Item #" & i)
    Next
For the Recyclerview a complete dataset is made available at start and the adapter is then notified.
B4X:
Sub arvFill
    DataSet.Initialize
    For i = 0 To 9
        Dim arvItemX As arvItem
        arvItemX.Initialize
        arvItemX.id = "i" & i & "_" & DateTime.Now  ' some mockup id
        arvItemX.Title = "Entry " & i
        arvItemX.Description = "Lorem ipsum to entry " & i
        DataSet.Add(arvItemX)
    Next
    arv.Adapter2.NotifyDataSetChanged
End Sub
For future work with Recyclerview it is important to understand this difference. After changes have been made to the underlying data, the display and clicked elements are only handled correctly if the adapter and dataset interact correctly.

If there is something substantially wrong in the above statements so that a wrong understanding would arise, I would ask you to tell me what needs to be changed in concrete terms.
 

Attachments

  • Events_AmirRecyclerView.jpg
    Events_AmirRecyclerView.jpg
    107.7 KB · Views: 552
Last edited:
Top