Search code examples
androidscrollandroid-listfragmentandroid-tablayout

How to create a scrollable list of items to use with a TabLayout?


I'd like to first note that this is my first ever proper android app I'm making (I've only followed tutorials so far), so please be detailed in your answers and forgive me if I'm spouting nonsense :).

I'm trying to create an app where the main activity is going to be a TabLayout, and in one of the tabs I want to have a fragment that is a list which you can scroll up and down, and also I would like each element in the list to have multiple interactive elements (like two different buttons for example).

My first instinct was to go with a ListFragment because each element of the list will be a fragment that you can program to do whatever you want. However when following tutorials I found that ListFragments seem to be rather tricky, and I'm not sure if I can make it work with a TabLayout.

I've looked for other methods, but I've only found the kinds of solutions that allow you to have a list of just plain views, not fragments.

So what should I do? Is there a way you could make a ListFragment work with a TabLayout and I'm just being silly, or is there a better way to do this?


Solution

  • My first instinct was to go with a ListFragment because each element of the list will be a fragment that you can program to do whatever you want

    The contents of a ListFragment will not be fragments. It is possible that the contents of a ListFragment will represent model objects for which you plan on showing other fragments elsewhere in your UI.

    I'm not sure if I can make it work with a TabLayout

    A ListFragment can be used inside or outside of a TabLayout. I would not think that a ListFragment inside of a TabLayout would be particularly more troublesome than any other sort of fragment.

    Your ListFragment problems will come from:

    I would like each element in the list to have multiple interactive elements (like two different buttons for example).

    ListView, which underlies ListFragment, is tricky to use with interactive elements in list rows. As your design sounds rather unusual, you might wish to consider using other UI patterns rather than multiple buttons in list rows. If you insist upon this design, you may find RecyclerView to be a bit easier in the long run, even if it is more challenging at the outset.

    I've only found the kinds of solutions that allow you to have a list of just plain views, not fragments

    That's the only thing you are ever going to find, in all likelihood. Again, ListFragment is a fragment that contains a ListView. ListView rows are not fragments, whether the ListView is contained in a Fragment or not. The only thing that I have ever heard of that is designed for vertical scrolling of fragments would be the various vertical ViewPager implementations floating around.