1

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).

rslemos
  • 2,454
  • 22
  • 32
  • I'm not clear on what you are trying to do. Do you want the two buttons to remain fixed at the bottom of the screen or do you want then to scroll with the _RecyvlewView_ but at the bottom of all entries or something else entirely? – Cheticamp Oct 21 '22 at 02:02
  • I want the two buttons to scroll along with the list. Positioned after the last item. Offscreen at first if said list is big enough. See, the actions entailed by these buttons are not very important (that's why I'll later change them to be links instead of buttons); to let them visible all the time is overkill. – rslemos Oct 21 '22 at 02:21
  • Take a look at [this](https://stackoverflow.com/questions/29106484/how-to-add-a-button-at-the-end-of-recyclerview). – Cheticamp Oct 21 '22 at 02:30
  • 1
    I think you are almost there but missing the concept. You add 2 bogus data, append it in your array and in your adapter you make sure to detect these 2 bogus data and use it as an indicator to add the items programmatically. And not through XML. – Lawrence Gimenez Oct 21 '22 at 02:36
  • @Cheticamp thanks for pointing out that question. Very interesting. I was rewriting my post and even used the clause "heterogeneous list with different types". I could never think that it was exactly that way it was implemented! Well, it looks hacky to me. But if this is the usual android way, I'll code it and move on. – rslemos Oct 21 '22 at 02:39
  • @LawrenceGimenez yes... but even better, instead of 2 bogus items, I can add 1 bogus item, that points to a layout with whatever I want as footer (in my case: 2 links/buttons). – rslemos Oct 21 '22 at 02:40
  • That is exactly it. – Lawrence Gimenez Oct 21 '22 at 02:42
  • I've got it almost working: when I don't override my adapter's `getItemCount` (to return `super.getItemCount() + 1`) then everything works fine (but I actually lose my last item, "overwritten" by my fixed footer). When I do override `getItemCount`, the app crashes. Maybe I shouldn't touch `getItemCount` in a subclass of `ListAdapter`. – rslemos Oct 21 '22 at 03:20

1 Answers1

1

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.

Arsh
  • 279
  • 2
  • 6
  • I was toying with composables but couldn't manage to get the theme right (the upper case text for buttons, for example). So I abandoned the idea for the time being. I expect it to get more mature tough. – rslemos Oct 21 '22 at 03:02
  • It is a bit difficult to get comfortable with compose at start. But if you practice and play around with it and get familiar with it you will realise it is actually easier to do certain things with compose than xml. It is similar to fast typing your speed decreases at first but when you practice, you achieve greater speed. – Arsh Oct 21 '22 at 03:06
  • You are a beginner so i will recommend you practice with xml only but once you get settled and comfortable with doing most of the things in xml you can move onto compose but definitely learn how to do things in xml too – Arsh Oct 21 '22 at 03:09