How can I scroll the last item in the RecyclerView to the top position?

Apologies in advance, I don't have any code examples as I am completely in the dark for solving my problem. I want to know how can I scroll the last item of the list to the top view position of the recycler?

By default, if all items are displayed and no scrolling is required, you can no longer scroll to see more items. The conflict with this is that I am updating the information in a separate view based on the topmost element.

Any ideas for this? The only thing I could think of would be to add arbitrary, fake blank data so that I can continue scrolling. thanks in advance

UPDATE: The best alternative solution, besides dummy data, is to add padding to the RecyclerView. If I add paddingBottom you won't see any blank space until you get to the last item. But the spacer must be accurate so that the last element is in the top position. So even this is a disadvantage.

<android.support.v7.widget.RecyclerView
    android:clipToPadding="false"
    android:paddingBottom="300dp"
    android:id="@+id/rv_data"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:divider="@null"
    android:scrollingCache="false"/>

      

+3


source to share


1 answer


Ok, I was able to get this to work. I first measure the size of the item in the adapter in onBindViewHolder

holder.llWrapper.measure(0,0);

      

I am getting a MeasuredHeight () from this and passing this back from my adapter to my fragment. Then I measure the viewing height of the recycler. I subtract the height of the recyclerview and the item and the remainder is the amount of padding I need to set from the bottom in the recyclerview



rv.setPadding(0,0,0,padding);

      

This is the best I can think of. Hooray!

0


source







All Articles