When using Recyclerview to display pictures or other information, often need to show a lot of item, when rolling to the bottom and want to go back to the top, if 1.1 points of the upward row to be more troublesome, and the user experience is not good. Therefore, it is necessary to add a quick button back to the top, and this button is hidden at the beginning and will not appear when the slide is more than one screen, and it will not appear during the sliding process. A lot of similar projects will be used, but how to get back to the top of the process without stalling, it is very smooth, this is very important. Here's a quick way to get back to the top of the class that I've modified based on my online approach, so I'll take a look at it later:
1.fastscrollmanger.java
import android.content.context;import android.graphics.pointf;import android.support.v7.widget.linearlayoutmanager;import android.support.v7.widget.linearsmoothscroller; import android.support.v7.widget.recyclerview;import android.util.displaymetrics;/** * Quickly return to the top of the recyclerview without stuttering */public class FastScrollManger extends Linearlayoutmanager { public fastscrolllinearlayoutmanager (Context Context) { super (context); } public fastscrolllinearlayoutmanager (Context context, int orientation, boolean reverselayout) { super (context, orientation, reverselayout); } @Override public void smoothscrolltoposition (Recyclerview&nbsP;recyclerview, recyclerview.state state, int position) { LinearSmoothScroller linearSmoothScroller = new Linearsmoothscroller (Recyclerview.getcontext ()) { @Override public pointf computescrollvectorforposition (int targetposition) { return FastScrollLinearLayoutManager.this.computeScrollVectorForPosition (targetposition); } //control speed. @Override protected Float calculatespeedperpixel (Displaymetrics displaymetrics) { return super.calculatespeedperpixel ( Displaymetrics); } @Override protected int calculatetimeforscrolling (INT DX) { if (dx > 3000) { dx = 3000; } int time = super.calculatetimeforscrolling (DX); return time; } }; linearsmoothscroller.settargetposition (position); startsmoothscroll (Linearsmoothscroller); }}
2. Use
Mrecyclerview = (Recyclerview) Findviewbyid (R.id.activity_recyclerview); Linearlayoutmanager layout = new Fastscrollmanager (Customactivity.this, linearlayoutmanager.vertical, false); Mrecyclerview.setlayoutmanager (layout);//Vertical placement ... mrecyclerview.setadapter (mmyadapter); Mrecyclerview.addonscrolllistener (New Myrecyclerviewscrolllistener ());
3. Control display and hide
Sliding monitoring private class MyRecyclerViewScrollListener extends recyclerview.onscrolllistener { @Override public void onscrollstatechanged (RecyclerView recyclerView, int newstate) { Super.onscrollstatechanged (recyclerview, newstate); LinearLayoutManager manager = (Linearlayoutmanager) Recyclerview.getlayoutmanager (); int firstvisibleitemposition = manager.findfirstvisibleitemposition (); // When not scrolling if (newstate == recyClerview.scroll_state_idle) { // determine if scrolling exceeds one screen if (firstvisibleitemposition == 0) { Mimageviewrebacktop.setvisibility (view.invisible); } else { mimageviewrebacktop.setvisibility ( view.visible); } } else if (newState == recyclerview.scroll_state_draggING) {//drag in mimageviewrebacktop.setvisibility (view.invisible); } } }
4. Click Back to the top button to return to the top
Mrecyclerview.smoothscrolltoposition (0);
Attached: the persistent thought ~
650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M02/9C/69/wKioL1lwKFWiyt9kAAEwbqHvIJs739.jpg "title=" 19a839c787e7bd823c270c9e30ff458a.jpg "alt=" Wkiol1lwkfwiyt9kaaewbqhvijs739.jpg "/>
This article is from the "Liangxiao Technology Center" blog, please be sure to keep this source http://liangxiao.blog.51cto.com/3626612/1949249
Android Studio-43rd phase Recyclerview when there is a lot of item, when you scroll to the bottom, quickly slide to the top