Scroll RecyclerView Scroll to position always on top - android

I'm using linear layout manager and RecyclerView with a LinearLayout Manager to populate some list of items. When I'm displaying the recyclerview for the first time and I use:
linearLayoutManager.scrollToPosition(desiredindex);
it scrolls to the top exactly where I want.
Now here is the tricky part - When I'm scrolling to top of recyclerview (i.e. new items indices will be lower than the desiredindex) and I call:
linearLayoutManager.scrollToPosition(desiredindex);
It still works fine, but when the recyclerview has been scrolled beyond the desiredindex, the recycler view scrolls such that the desiredindex item comes to the bottom rather than on top, but I want the tile to scroll to the top not the bottom.

Use scrollToPositionWithOffset like this:
linearLayoutManager.scrollToPositionWithOffset(desiredindex, 0);
scrolltopositionwithoffset(position, offset) forces the indicated item visible with indicated offset. The offset is distance from the top of RecyclerView.

Related

Sticky item in Android's RecyclerView

I have a recyclerView with multiple viewTypes, is there any way using which I can stick a particular viewType on top of the recyclerView once it reaches there after getting scrolled up?
This viewHolder initially appears at the bottom but once user scrolls up and the view reaches on top I want it to stick there and on scroll down naturally comes down to original position.

Overlap items in recyclerView

I have a RecyclerView with list of items displayed top to bottom.
I would like to have view of last item overlapping with the previous one.
I used RecyclerView.ItemDecoration with negative top margin and it almost does the trick. Item views indeed overlap, but when I'm at the bottom of the list and I start scrolling up then overlapping view disappears too early.
It looks like RecyclerView does not take into account the negative margin at all when deciding if item is visible or not.
Is there a way to fix that?
Thanks,

How to make last item scroll all the way to the top of RecyclerView

I have a RecyclerView holding TextViews in its rows. When you scroll all the way down, the scrolling stops so that the last item's bottom is aligned with the RecyclerView's bottom. Is there an easy way to make the last item able to scroll so that it's top is aligned with the RecyclerView's top?
This is a screenshot of default behavior when scrolling all the way down:
This is the desired behavior:
What I have tried:
I have calculated the heights of all the other views besides the RecyclerView and subtracted this value from the dynamic screen height and added this value to the bottom padding of the last item in the RecyclerView. But for some reason the dimensions are pushing the last item out of view.
The heights I measured are:
status bar height, the app bar height, the 3 header heights, the bottom footer bar height, the bottom navigation bar height (not seen in screenshot), and the height of the last item itself
I have looked at other similar questions/answers (e.g. scroll recyclerview item to top inside of scroll view, Allow Recyclerview Items to scroll past top of Recyclerview, Android what does the clipToPadding Attribute do?) but no success.
I need it to work in both portrait and landscape mode.

Allow Recyclerview Items to scroll past top of Recyclerview

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.
Then I select an item
I then want the selected item to scroll to top, but for the item above to still be scrollable.
So, when I scroll up...
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.
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

Scrolling an item to the top position is not working in RecyclerView for few items in the adapter

I want to scroll a specific item to the top in RecyclerView.
Here is the code I am using,
rvTweets.smoothScrollToPosition(position);
or
linearLayoutManager.scrollToPositionWithOffset(postion,0);
Both the code work when RecycelerView has enough items to cover the complete height of the screen. Otherwise, it does not scroll the desired position to the top.

Categories

Resources