0

When a new item is added, it's populated from the bottom of the UI, so I tried some methods from LinearLayoutManager setReverseLayout() == true but items stack from bottom. I tried different combinations of reverseLayout and stackFromEnd, but still no luck, the new item is still populated from the bottom of the UI, second item is populated below the first item, and all the way stack to the top.

What I want is that the first item will be populated on the top of the UI, not bottom, and then the second will be below the first item etc..

        val recyclerView = findViewById<RecyclerView>(R.id.recyclerview)
        val adapter = SomeListAdapter(Some params)

        val llManager = LinearLayoutManager(this)
        llManager.reverseLayout = true
        llManager.stackFromEnd = true

        recyclerView.adapter = adapter
        recyclerView.layoutManager =llManager
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/layout"
    tools:context=".AddressActivity">

    <RelativeLayout
        android:id="@+id/rl_address"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="60dp"
        android:background="@color/black"
        android:backgroundTint="#FEFBDE"
        android:orientation="vertical"
        android:padding="5dp">
        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            tools:listitem="@layout/address_item"
            android:layout_above="@+id/ll_addAddress"
            />

        <RelativeLayout
            android:id="@+id/ll_addAddress"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_marginTop="1dp">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <Button
                    android:id="@+id/addNewAddress"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"

                    android:text="Add New Address">

                </Button>
                <Button
                    android:id="@+id/viewAllAddress"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="View all addresses">

                </Button>

                <Button
                    android:id="@+id/welcome_landlord"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="Welcome Landlord" />

            </LinearLayout>
        </RelativeLayout>
    </RelativeLayout>

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/navigation_menu"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_alignParentBottom="true"
        app:labelVisibilityMode="labeled"
        app:menu="@menu/bottom_navigation_menu">

    </com.google.android.material.bottomnavigation.BottomNavigationView>

</RelativeLayout>

enter image description here

zxcisnoias
  • 494
  • 3
  • 19

1 Answers1

2

Unless I'm missing something you just want the standard behaviour? Just don't set reverseLayout or stackFromEnd as true, the whole point of those is to reverse the order of items and start filling from the bottom/end.

If your RecyclerView still isn't behaving how you want, it's probably because of how you've set up your layout. You're using a RelativeLayout where ll_addAddress is pinned to the bottom, and then your RecyclerView is positioned above that with android:layout_above="@+id/ll_addAddress". The result is that your RV is pinned to the bottom of the available space.

So because your RecyclerView's height is wrap_content, the top of the RV (which isn't pinned) grows upward as you add items. This means that when you add your first item, it's at the top of the RV by default, but the top of the RV is near the bottom of the space, just high enough to accommodate the item you added. So unless you can see the actual bounds of the RV, it looks like you're adding items at the bottom of it, when you're not. (You can use the Layout Inspector to see this happening on a running app, or add a background to the RecyclerView so you can see what's happening to it.)


The solution is that you really need to pin the top of the RecyclerView to something, so it takes up a fixed space in your layout. It sounds like you already have an expectation of where "the top" is supposed to be, but in your layout it actually depends on the height, which depends on the contents.

This is also gonna cause you problems when you add enough items that the RV needs to actually scroll - it won't, because you're not constraining its height, so it will just continue to grow offscreen. Scrolling views only start to scroll when their height is less than the height of their contents, which requires you to limit that height somehow - setting their height to wrap_content is a red flag.

It's been a long time since I used a RelativeLayout, so you might be able to just align the top with the parent too, but I'd really recommend you use ConstraintLayout instead - it's the modern way to develop flexible layouts, and it allows you to easily do things like constrain the top and bottom to certain views, allowing you to fill the available space. Either way, you have to constrain the height of that RecyclerView somehow

cactustictacs
  • 17,935
  • 2
  • 14
  • 25