10

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.

guness
  • 5,123
  • 7
  • 50
  • 79

2 Answers2

17

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.

frozenkoi
  • 3,118
  • 19
  • 32
2

It seems to Android don't recognize app:stackFromEnd="true". Maybe you put it in wrong place. setStackFromEnd(Boolean) does the same job, but programmatically.

According to documentation:

public void setStackFromEnd(boolean stackFromEnd)

Compatibility support for setStackFromBottom(boolean)

and

public void setStackFromBottom (boolean stackFromBottom)

Added in API level 1 When stack from bottom is set to true, the list fills its content starting from the bottom of the view.

Parameters stackFromBottom true to pin the view's content to the bottom edge, false to pin the view's content to the top edge

Read these:

http://developer.android.com/reference/android/support/v7/widget/LinearLayoutManager.html#getStackFromEnd()

http://developer.android.com/reference/android/widget/AbsListView.html#setStackFromBottom(boolean)

piotrek1543
  • 18,034
  • 7
  • 69
  • 86
  • 1
    It is apparent that android doesn't recognise `app:stackFromEnd="true"`. The question is why? – guness Dec 15 '15 at 21:01