Search code examples
androiddata-bindingviewandroid-recyclerviewauto-generate

Generating views for object with lists of data or using RecyclerView?


I have a Detail Activity. Lets say I have this object I want to show to the user.

public class Dummy {
    private String name;        
    private ArrayList<String> telephoneNumbers;
    private String somethingElse;
}

Please note this is only an example to get on my point. Real object is way more complicated

Now, because I need to have name at the top, list of telephone numbers each in it's own view stacked vertically and below them i want to have something else.

Which means I have two options.

1. Generate views programmatically into some ViewGroup

<LinearLayout orientation="vertical">
    <name />
    <LinearLayout orientation="vertical">
        Here I will programmatically generate one view per telephone number
    </LinearLayout>
   <somethingElse />
</LinearLayout>

This approach is pretty straightforward. But downside is when I will have 100 phone numbers - low performance because of 100 possibly complicated layouts, 100 click listeners or whatever I would do here.

(Also side question - is this possible to do with Android Data binding?)

2. Use RecyclerView for the whole thing

It shouldn't be that hard to write adapter that takes one instance and uses three types of viewHolders (one for name, another type for telephone numbers, and third type for somethingElse)

ItemCount would be telephoneNumbers.size() + 2. And then only thing remaining to do is to do some calculation for each adapter position to create appropriate viewHolder and bind data to it.

What do you guys think is the best approach?


Solution

  • I think you should use method 2, first, I have done similar things already, if you can do method 1, it would not be hard for you to implement method 2.

    And most importantly, you might have answered it yourself, if you plan to support say 100+ phone number, it is possible to introduce performance issue with method 1.

    And method 2 is way cleaner in code.