android - ReyclerView - programmatically scroll with predefined time -
i need dragging similar action on recyclerview
, when reach top or bottom end of recyclerview
want scroll smoothly...
i following: post runnable scroll until user not near enough start/end of recyclerview
anymore.
mscroll = new runnable() { @override public void run() { rvimages.smoothscrollby(0, mscrolldist.get()); mhandler.postdelayed(mscroll, 100); } };
the problem, need define, scrolling desired distance should done in 100ms, overall scrolling smooth.
how can achieve that?
use custom linearlayoutmanager
public class mycustomlayoutmanager extends linearlayoutmanager { //we need mcontext create our linearsmoothscroller private context mcontext; public mycustomlayoutmanager(context context) { super(context); mcontext = context; } //override method? check. @override public void smoothscrolltoposition(recyclerview recyclerview, recyclerview.state state, int position) { //create recyclerview.smoothscroller instance? check. linearsmoothscroller smoothscroller = new linearsmoothscroller(mcontext) { //automatically implements method on instantiation. @override public pointf computescrollvectorforposition(int targetposition) { //what pointf? class holds 2 float coordinates. //accepts (x , y) //for y: use -1 direction, 1 down direction. //for x (did not test): use -1 left direction, 1 right //direction. //we let our custom linearlayoutmanager calculate pointf return mycustomlayoutmanager.this.computescrollvectorforposition (targetposition); } }; //docs not tell this, //but need set position want scroll to. smoothscroller.settargetposition(position); //call startsmoothscroll(smoothscroller)? check. startsmoothscroll(smoothscroller); } }
customizing scrool speed
//make instance variable @ top of custom layoutmanager private static final float milliseconds_per_inch = 50f; . . . linearsmoothscroller smoothscroller = new linearsmoothscroller(mcontext) { . . . //the holy grail of smooth scrolling //returns milliseconds takes scroll 1 pixel. @override protected float calculatespeedperpixel (displaymetrics displaymetrics) { return milliseconds_per_inch / displaymetrics.densitydpi; } };
in activity:
mlayoutmanager = new mycustomlayoutmanager(getactivity()); mrecyclerview.setlayoutmanager(mlayoutmanager); mrecyclerview.smoothscrolltoposition(position);
Comments
Post a Comment