6

I have a recyclerview with several items. I need to show the items starting from bottom i.e. first item should be displayed at bottom and after scrolling from bottom to top, further items will get displayed. For this what I tried is,

linearlayoutManager.setStackFromEnd(true);

This helped to start the items from bottom. But when there are several items, more than the size to fit into the screen at the same time then the first item goes moving at the top as the item count increases.

I also tried with

linearlayoutManager.scrollToPosition(0);

This didn't change anything.

Here is an image showing what I am looking for.

enter image description here

There are few more items in the list above which are still not visible.

Here how it looks When these items are scrolled, enter image description here.

I have checked view hierarchy of this view in the image which only shows a recyclerview and the items within it. And the recyclerview's height is match_parent i.e. the top space with no items showing another view underneath of the recyclerview which is a google map. I just need to know how I can achieve this feature of the recyclerview without using more extra views.

Any help would be greatly appreciated..!!

App developer
  • 158
  • 1
  • 1
  • 10
  • 1
    Did you solve the issue? If yes, please post the solution so it could help other developers looking for solns. – sanjeev Dec 28 '18 at 04:59

7 Answers7

5

Try to init your Linear layout as follow

RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, true);

The third boolean indicates that it should reverse the items or not. But remember that your datas should be sorted from newest to oldest also.

  • Thanks for your response. I tried your line of code. But it doesn't work to what I am expecting. I need to get the first item at the bottom initially once it is launched and then it should be scrolling to top for next items. – App developer Apr 20 '17 at 09:55
  • what do you mean by "scrolling to top for next items"? – Lê Thái Phúc Quang Apr 20 '17 at 09:59
  • 1
    Please go through both the images I have posted in the question. First image shows the initial state of the recyclerview, showing only 1-2 out of many items. And after dragging/scrolling those items to top, it shows further items from the list. – App developer Apr 20 '17 at 10:07
3
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, true);
linearLayoutManager.setStackFromEnd(true);
linearLayoutManager.setReverseLayout(true);

Then once your data is populated

adapter.notifyDataSetChanged();
recyclerView.post(new Runnable() {
@Override
public void run() {
   recyclerView.smoothScrollToPosition(0);
    }
});
Robert Goodrick
  • 190
  • 1
  • 11
2

Try using a reversed LinearLayoutManager:

LinearLayoutManager linearlayoutManager = new LinearLayoutManager(context, VERTICAL, true);
Bartek Lipinski
  • 30,698
  • 10
  • 94
  • 132
1

write this line in xml to start list from bottom

 android:stackFromBottom="true"

 <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:stackFromBottom="true">

    </android.support.v7.widget.RecyclerView>

and second thing reverse your array list before add in adapter

Hitesh Gehlot
  • 1,307
  • 1
  • 15
  • 30
  • Thanks Hitesh. It is stackFromEnd which is used to add items from the bottom for recyclerview layout manager. I already tried that and also used to reverse the arraylist but anyhow list items are filled up and the items are displayed full of screen. – App developer Apr 20 '17 at 10:03
1

Add these attributes to your recyclerview (XML)

android:paddingTop="500dp"          
android:clipToPadding="false"

You can also set it in the code like this:

recyclerView.setPadding(0, 500, 0, 0);
recyclerView.setClipToPadding(false);

This is for different screen sizes

int height = recyclerView.getHeight()
recyclerView.setPadding(0, height - 200, 0, 0);
recyclerView.setClipToPadding(false);

If you don't know how to get screen width, height and convert dp to px

so here are the links: How to get screen width and height and Converting pixels to dp

Hamed Ghadirian
  • 6,159
  • 7
  • 48
  • 67
Zheentoro
  • 37
  • 9
0

Seems like what you want is to have a separate view type in your recycler adapter that doesn't show anything but is the height of the map. This would allow you to 'scroll up' from the bottom where the list starts and continue seeing different views.

A simple example:

public class ScreenBottomRecyclerAdapter extends RecyclerView.Adapter {
    final int VIEW_TYPE_MAP = 0;
    final int VIEW_TYPE_ROW = 1;

    @Override
    public int getItemCount() {
        return dataset.length + 1;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return VIEW_TYPE_MAP;
        } else {
            return VIEW_TYPE_ROW;
        }
    }

    @Override
    public RecyclerView.ViewHolder createViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case VIEW_TYPE_MAP: return new ViewHolder(null); // This is an empty recycler row so you can see and interact with the map, it will need to be the height of the map so you'll probably want to make a new view type that the same height but doesn't show anything and doesn't intercept touches
            case VIEW_TYPE_ROW: return new RowViewHolder(someInflatedView); // This will be the rows you are already creating
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        switch (getItemViewType(position)) {
            case VIEW_TYPE_ROW: holder.update(position - 1) // Do your normal update here, but remember that your data will be off by 1 for the map view row in the recycler
        }
    }
}
ckern
  • 176
  • 2
  • 5
0

Use below code to reverse list:

LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity(), RecyclerView.VERTICAL, false);

Important: Set layout manager which have property reverse layout to true

layoutManager.setReverseLayout(true);

RecyclerView.setLayoutManager(layoutManager);

This code will display reverse list which you may have assigned to RecyclerView

Jitesh Prajapati
  • 2,533
  • 4
  • 29
  • 51