4

I have a RecyclerView list, in which I want the currently selected item to be shown at the top of the RecyclerView. I still however, want the entire list to be scrollable, therefore, removing views from above the selected item is not a possible solution.

It seems I need a mechanism where the RecyclerView items are able to scroll beyond the bounds of the RecyclerView. I'm not sure if this is possible, so if it is not, does anyone have a solution to ensuring the currently selected item scrolls to the top of the RecyclerView.

I have tried smoothScrollToPosition() but this doesn't work in the case of being at the bottom of the RecyclerView, and wanting one of the middle items to scroll to the top.

Many thanks

to Illustrate, I have a list of 4 items, the recyclerview cannot scroll as there is not enough items in the list.

enter image description here

Then I select an item

enter image description here

I then want the selected item to scroll to top, but for the item above to still be scrollable.

enter image description here

So, when I scroll up...

enter image description here

5
  • everything is possible my frnd..just remove the selected item from the list and add it at the first position of the list and then notifyItemRangeInserted(0, selected_position); recyclerView will auto scroll to top... :) Commented Feb 28, 2018 at 16:16
  • Thanks for your comment, but I still want the items above the selected item to be scrollable to, I don't think this fixes that. Commented Feb 28, 2018 at 16:17
  • you want autoscroll or you want the user to scroll and you want keep the selected to its original position or do you want it at the top? Commented Feb 28, 2018 at 16:19
  • got it my frnd :) working on it Commented Feb 28, 2018 at 16:22
  • you can delete all items till the selected position from the list and notifyDataSetChanged() and when the user tries to scroll up add those elements and again notifyDataSetChanged() Commented Feb 28, 2018 at 16:32

2 Answers 2

7

On the RecyclerView set a bottom padding that is equal to three times your item's height then set android:clipToPadding="false". This will let your bottom item scroll to the top and show the padding on the bottom item but only on the bottom item.

Here is an answer to a similar question that lays this technique out rather well.

Sign up to request clarification or add additional context in comments.

1 Comment

Excellent! Thank you
0

lets assume that you have item on click listener for your recycler view, when user clicks on any item you get item position and view, below code is working for me

RecycleClick.addTo(firstRecyclerView).setOnItemClickListener(new RecycleClick.OnItemClickListener() { @Override public void onItemClicked(RecyclerView recyclerView, int position, View v) { // YOUR CODE int offset = position - yourRecyclerViewLayoutManager.findFirstVisibleItemPosition(); if (yourRecyclerViewLayoutManager.findFirstVisibleItemPosition() > 0) offset -= 1; yourRecyclerViewLayoutManager.scrollToPositionWithOffset(position, offset); } }); 

make sure your layout manager comes from support library like this

android.support.v7.widget.LinearLayoutManager 

otherwise you will not find findFirstVisibleItemPosition() method etc

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.