rslemos
rslemos

Reputation: 2731

Scrollable list trailed by fixed views

This might be a very beginner question, but I'm yet unable to find myself around the android jungle.

I've already got a RecyclerView working to show a list of items (with data binding and Room database and DiffUtil.ItemCallback and all).

I'd like to put 2 links after the list: "missing something?" and "add new entry" that will lead to other fragments.

What I have:

When I put 2 buttons (I don't know yet how to put links, but this is not the point of this question) after the RecyclerView, all in a LinearLayout, they stay fixed near the screen bottom. I mean, the RecyclerView is scrollable by itself, scrolling "beneath" the two buttons, the entire LinearLayout expanding to fill the screen (match_parent).

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
            <androidx.recyclerview.widget.RecyclerView
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:gravity="top"
                app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="Missing something?"
                android:onClick="@{...}" />
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="Add new item"
                android:onClick="@{...}" />
        </LinearLayout>

What I want

I'd like the 2 buttons to scroll along with the list, so that they are always positioned after the last item (think as if they were items themselves, albeit an heterogeneous list with different types/RecyclerView.ViewHolder).

For a big enough list the buttons will be initially off screen; to be scrolled in if the user happen to scroll to the bottom of the list.

What I tried

I tried with ScrollView around the LinearLayout, and it works, but everywhere everybody say that one should never put a RecyclerView inside a ScrollView (maybe because it is scrollable itself).

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/routines_list"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="top"
                app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
            <!-- buttons -->
        </LinearLayout>
    </ScrollView>


Being really a beginner in android programming, I'd like to know how usually this kind of layout should be done. Only main directions will be enough for me.

NB. I don't know if I really need a RecyclerView because I don't expect this list to be lengthy. Maybe usually something around 4 to 8 items, possibly 10. But I really don't expect it to be much bigger than that. For many users the two links will even be visible all the time (i.e. no scroll at all).

Upvotes: 1

Views: 31

Answers (1)

Arsh
Arsh

Reputation: 274

RecyclerView is always the most efficient to show a list especially if you are getting the data from a database or an API. Don't put your recyclerview in a scrollview. You can add two items to the bottom of the list as your links and program your recyclerview to exhibit different properties for last two items. That is the best way I can think of. Good Luck!

Also, Recyclerview is very difficult to work with when you are working with complex data. With small lists such as in your case, it can seem inconvenient to create a whole adapter class and do everything you are supposed to do. When you have grasped the concepts on xml android and have plenty experience with that. You can move to jetpack compose and lazy column will make your life easy.

Upvotes: 1

Related Questions