I want to scroll a list view by x pixels
list.scrollListBy(x) works, but the default scrolling animations are missing
Is there any api to scroll list with animation,
I cannot use smoothScrollBy and its other varients, because it wont scroll if the element is already visible.
If you are using RecyclerView, you can call the method
list.scrollToPosition(int position)
to scroll to a particular position on the list. Let's say there are 50 positions on the list, and your phone screen is showing first 10 positions of the list. Then you can call
list.scrollToPosition(40);
to scroll to the 40th position. There will be a slightly smooth effect.
Related
I'm trying to programatically scroll to an item in my RecyclerView based on position.
The following code works when scrolling to an item in the RecyclerView adapters data if the position is visible on screen / rendered:
recyclerView.getLayoutManager()).scrollToPositionWithOffset(position, 0);
However if I set a position to a value which is not yet visible on the screen / rendered, the page is only scrolled just below the last item which is visible / already rendered. E.g. it works for the first 4 items which are visible, but not the 4 which are off screen.
For extra details the data in the adapter is set in an onSuccess callback:
// update the data in the adapter after hitting a REST API
adapter.updateItems(data);
adapter.notifyDataSetChanged();
// then I scroll to the position
recyclerView.getLayoutManager()).scrollToPositionWithOffset(position, 0);
Other methods to invoke the scrolling haven't worked - e.g. just calling recyclerView.scrollToPosition(position)
And the view is rendered inside a BottomSheetDialog.
There seem to be quite a few questions relating to similar things which have not solved my problems - eg RecyclerView - How to smooth scroll to top of item on a certain position?
UPDATE
Actually trying it with a different longer data set its not only the fact that the item position is not visible - it appears to only be the last items which don't scroll correctly (e.g the last three).
Thanks!
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.
I have tried RecyclerView.smoothScrollToPosition(int position), but it's not what I want. It scrolls to a position so that the child get into the viewport; it can either appear at the beginning or the end. Moreover, if the view is already in the viewport, it won't do anything!
What I need to do is to scroll to a position and make it appear at very beginning of RecyclerView so as to arouse user's attention. How can I do with it?
You can use mRecyclerView.scrollToPosition(Position); to display the list item at top. However it will go to the specified position very quickly without any scrolling effect.
I want to give animation in GridView. So that when the list gets prepared, it will scroll to given position without touching the screen.
You can use the methods smoothScrollToPosition and smoothScrollToPositionFromTop to make the GridView go to that position.
I have a ListView in which the user can drag&drop list elements to reorder the list (android 3.0). However, I have to handle the case when the list is long, and the user has to be able to scroll it during dragging an item. So I've put two scroll-areas onto the screen, one at the top and one at the bottom. When the user drags an item onto these, the ListView should start to scroll up/down with a constants speed, as long as the dragged item is in these areas.
How can I achieve this scrolling with ListView?
I've tried the smoothScroll...() methods but they need a specific position in the list to scroll to, or a specific distance to scroll by. The standard scrollBy...() methods don't work either, they scroll the ListView, but don't appear to invalidate is properly, so the appearing rows don't get rendered.
Any ideas?
You could try listView1.smoothScrollByOffset(int viewPosition). This way you could make the listview scroll up and down by an interval of 1+ views.
You could try smoothScrollToPositionFromTop(int position, int offset, int duration) with varying duration depending on the position where to scroll, like say
int duration = position*100;