android


RecyclerView - How to smooth scroll to top of item on a certain position?


On a RecyclerView, I am able to suddenly scroll to the top of a selected item by using:
((LinearLayoutManager) recyclerView.getLayoutManager()).scrollToPositionWithOffset(position, 0);
However, this abruptly moves the item to the top position. I want to move to the top of an item smoothly.
I've also tried:
recyclerView.smoothScrollToPosition(position);
but it does not work well as it does not move the item to the position selected to the top. It merely scrolls the list until the item on the position is visible.
for this you have to create a custom LayoutManager
public class LinearLayoutManagerWithSmoothScroller extends LinearLayoutManager {
public LinearLayoutManagerWithSmoothScroller(Context context) {
super(context, VERTICAL, false);
}
public LinearLayoutManagerWithSmoothScroller(Context context, int orientation, boolean reverseLayout) {
super(context, orientation, reverseLayout);
}
#Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state,
int position) {
RecyclerView.SmoothScroller smoothScroller = new TopSnappedSmoothScroller(recyclerView.getContext());
smoothScroller.setTargetPosition(position);
startSmoothScroll(smoothScroller);
}
private class TopSnappedSmoothScroller extends LinearSmoothScroller {
public TopSnappedSmoothScroller(Context context) {
super(context);
}
#Override
public PointF computeScrollVectorForPosition(int targetPosition) {
return LinearLayoutManagerWithSmoothScroller.this
.computeScrollVectorForPosition(targetPosition);
}
#Override
protected int getVerticalSnapPreference() {
return SNAP_TO_START;
}
}
}
use this for your RecyclerView and call smoothScrollToPosition.
example :
recyclerView.setLayoutManager(new LinearLayoutManagerWithSmoothScroller(context));
recyclerView.smoothScrollToPosition(position);
this will scroll to top of the RecyclerView item of specified position.
hope this helps.
RecyclerView is designed to be extensible. There is no need to subclass the LayoutManager just to perform the scrolling.
Instead just create a SmoothScroller with the preference STAP_TO_START:
RecyclerView.SmoothScroller smoothScroller = new LinearSmoothScroller(context) {
#Override protected int getVerticalSnapPreference() {
return LinearSmoothScroller.SNAP_TO_START;
}
};
Now you set the position where you want to scroll to:
smoothScroller.setTargetPosition(position);
And pass that SmoothScroller to the LayoutManager:
layoutManager.startSmoothScroll(smoothScroller);

Related Links

Schedule notification using alarm manager in xamarin forms for android
I can't get my final textview to show up horizontal scrollview
android permission READ_SMS granted but not working (even using REQUEST_CODE_ASK_PERMISSIONS)
Android: Animation scale independent animation
Android - Loop random array and don't repeat
Is there any requirements to connect application with sms gateway to send or recive message online?
Force user to upgrade the application for further use
Adb emu command not working sometimes
How to make an Android REST client to post videos/images to a Jersey web service?
Android Share Dialog invalid post id
Layout Inflater using ViewPager throws NullPointerException
Android: Handler with MainLooper : “only the original thread that created a view hierarchy can touch its views.”
LinearLayout weight vs percentRelativeLayout android
Conversion buffer overflow when saving updated XML to file
how to copy files from android to windows share using python?
Android - PendingIntent.FLAG_CANCEL_CURRENT - does it really cancel alarmManager previous pending intent?

Categories

HOME
android
search
scikit-learn
turing-machines
checkstyle
gdb
c++11
time-complexity
css3
character-encoding
apache-spark-mllib
yahoo-finance
camera
owl-carousel
opendj
switch-statement
parameters
telegram-bot
bookmarks
echo
azure-container-service
odoo-8
nfs
modal-dialog
openstreetmap
enthought
distribution
mxgraph
3nf
azure-powershell
coin3d
google-content-api
pimcore
exit
interpreter
google-api-client
jboss6.x
google-cloud-print
maze
quantlib
constexpr
installscript
html-encode
filesystemwatcher
diacritics
metawidget
opensmpp
install.packages
ssrs-2014
tcc
drupal-theming
fwrite
jboss-4.2.x
openresty
sonarlint-eclipse
spring-data-couchbase
css-transforms
flac
ampps
docpad
actor-platform
lrs
pushbots
cycle2
user-management
toran-proxy
java-money
include-guards
poppler
canalyzer
sony-lifelog-api
lasso
sql-server-data-tools
ansi
node-serialport
diff3
rrule
openbabel
strawberry-perl
cuba
paste
mongohq
nevron
cascadingdropdown
wcf-configuration
subproject
floating-point-exceptions
filemerge
pylucene
mbeans
msr
build-environment
wysiwym
application-planning
commercial-application

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile