app:stackFromEnd for RecyclerView is not working in xml?

guness picture guness · Dec 15, 2015 · Viewed 7.6k times · Source

I have a RecyclerView defined as:

    <android.support.v7.widget.RecyclerView
      android:id="@+id/message_list"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:layout_above="@id/message_input"
      android:layout_alignParentTop="true"
      app:stackFromEnd="true" />

Related code is also common used one:

    LinearLayoutManager layoutManager = new LinearLayoutManager(this);
    //layoutManager.setStackFromEnd(true);
    mRecyclerView.setLayoutManager(layoutManager);

However when I add an item RecyclerView, it does not respect app:stackFromEnd="true". On the other hand, if I uncomment and use layoutManager.setStackFromEnd(true); programatically, it works fine. What is the problem I am missing? Any ideas are welcome.

Answer

frozenkoi picture frozenkoi · Apr 16, 2016

I ran into something similar. The problem is that when the RecyclerView is inflated it does read the attribute.

But then you're assigning a new LinearLayoutManager that is created in the java code. This new manager doesn't have the stackFromEnd attribute set to true (defaults to false).

stackFromEnd is an attribute of the manager, not the RecyclerView.

In my case I have this:

<android.support.v7.widget.RecyclerView
    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:id="@+id/list"
    app:stackFromEnd = "true"
    app:layoutManager="LinearLayoutManager"
</android.support.v7.widget.RecyclerView>

In your xml code you don't have the app:layoutManager attribute so I'm not sure what manager is created for you on inflation.