Allow Recyclerview Items to scroll past top of Recyclerview - android

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

Related

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.

ItemTouchHelper make dragged item go over fragment footer

I have a RecyclerView with an ItemTouchHelper. The RecyclerView is a 2/2 grid and it has a header and footer on the page. When the user picks up an item I want that item to be able to hover over the footer. However, I do not want the other items in the recycler view to be able to be seen through the footer.
So first I tried adding clipChildren and clipPadding but that created a situation where the RecyclerView items were scrolling up through the footer which was a problem. I tried adding elevation to the item and bringToFont but those never worked because the recycler view was still behind the footer, I tried adding bring to front to the recycler view itself but then once again all the items become in front of the footer again instead of just the dragged one, does anyone have any other ideas on how to solve this?
I would start by making sure your recycler view has android:clipChildren="false". That way you should not need the recyclerview to be literally overlapped by the footer - it can end above the footer and still allow it's children to draw outside of bounds.
If you have a static footer you can try calling bringToFront when you determine y axis has moved far enough that your current item intersects the footer. Then when you move back out of range or when the recyclerview scrolls at all, bring the footer back to the front. However this would be particularly error prone; a bit of a hack.
Edit:
A different approach would be to completely sever the connection between a dragged item and the recyclerview (this will take a bit of work on the drop part), the steps would roughly be:
- Start dragging item i
- store a reference to i.getParent() and the view index within its parent
- i.getParent().removeView(i) //detach the item from the recycler (or create clone)
- layout.addView(i) //add item onto main layout containing footer, allowing overlap
- when dropped, use the parent reference and index to reattach the item
- then work out where the item was released
Suggestion, in my custom TouchHelperCallback I overrided onMove method and ignored checking if variables source and type are the same. So when I was dragging item over header, I could put item over it.
#Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder source, RecyclerView.ViewHolder target) {
//if (source.getItemViewType() != target.getItemViewType()) {
// return false;
//}
// Notify the adapter of the move
mAdapter.onItemMove(source.getAdapterPosition(), target.getAdapterPosition());
return true;
}
Not modified tutorial about drag drop I took from: https://medium.com/#ipaulpro/drag-and-swipe-with-recyclerview-b9456d2b1aaf#.jl59rui0l

How to move the n-th invisible item of recyclerview to center

I'm using a recyclerView to support an item selection function for an android app, where each item of recyclerView will be a same sized image view. When creating the recyclerView, I already know that the n-th item is the selected one, how do I directly display the n-th item in center or just make it visible?
UPDATE:
So I achieved this by first calling linearLayout.scrollToPosition(n-th position) to make the item visible, then adding a addOnGlobalLayoutListener, which will wait for the layout to be complete, then calculate the offset and do a smooth scroll to center.
Use layoutManager.scrollToPositionWithOffset(n-th position, half width of screen).
It automatically scroll to n-th item position.

ListView.TRANSCRIPT_MODE_ALWAYS SCROLL in RecyclerView

As we know we have option ListView.TRANSCRIPT_MODE_ALWAYS SCROLL which will scroll to bottom.
The list will automatically scroll to the bottom, no matter what items
are currently visible.
Now we are having RecyclerView, does there any similar option of this in RecyclerView?
You can use
recycler_view.scrollToPosition(int position);
where position is index of row in recycle view.

Scroll RecyclerView Scroll to position always on top

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.

Categories

Resources