Reprint please indicate the source: Wang 亟亟 's way of Daniel
Because today is still a bit of work unfinished, so do not blow B, the introduction of the library after the explanation on the meal to eat, the afternoon to work
Infinitescroll
In the traditional ListView Li Youcai Hongyuan pulltofressh do a pull-up refresh of the implementation, today on a similar effect infinitescroll, but he is parked under the Recycleview more in line with the development trend
:
Pull in the end to add in more dialog effect, use a lot of scenes
How do I use?
Grade:
{ compile ‘com.github.pwittchen:infinitescroll:0.0.1‘}
Maven:
<dependency> <groupId>com.github.pwittchen</groupId> <artifactId>infinitescroll</artifactId> <version>0.0.1</version></dependency>
Eclipse: Copy the part of the circle.
Then the specific how to quote, the direct sticker example code in the detailed explanation
Public class mainactivity extends appcompatactivity { Private Static Final intMax_items_per_request = -;//Per request Item Count Private Static Final intNumber_of_items = -;//Maximum Item Count Private Static Final intSimulated_loading_time_in_ms = the;//Simulate time-consuming operation of sleeping threads PublicToolbar Toolbar; PublicRecyclerview Recyclerview; PublicProgressBar ProgressBar;PrivateMyadapter Myadapter;PrivateLinearlayoutmanager LayoutManager;Privatelist<string> items;Private intpage =0;@Override protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.activity_main);//Initialize data source This. Items = CreateItems ();//Get control IDInitviews ();//Initialize RecyclerviewInitrecyclerview (); Setsupportactionbar (toolbar); }//If <10, then front + "0" PrivateList<string>CreateItems() {list<string> items =NewLinkedlist<> (); for(inti =0; i < Number_of_items; i++) {String prefix = i <Ten?"0":""; Items.Add ("Item #". concat (prefix). Concat (String.valueof (i))); }returnItems }Private void initviews() {toolbar = (toolbar) Findviewbyid (R.id.toolbar); Recyclerview = (Recyclerview) Findviewbyid (R.id.recycler_view); ProgressBar = (ProgressBar) Findviewbyid (R.id.progress_bar); }Private void Initrecyclerview() {LayoutManager =NewLinearlayoutmanager ( This);//If item height is not changed, improve performance operationRecyclerview.sethasfixedsize (true);//Set ListView styleRecyclerview.setlayoutmanager (LayoutManager);//Add adapter, Sublist method intercepts the 0->max_items_per_request portion of the original 100-length ArrayListMyadapter =NewMyadapter (Items.sublist (page, max_items_per_request)); Recyclerview.setadapter (Myadapter);//Add slide monitorRecyclerview.addonscrolllistener (Createinfinitescrolllistener ()); Myadapter.setonitemclicklistener (NewMyadapter.onrecyclerviewitemclicklistener () {@Override Public void Onitemclick(View view,intPosition) {Toast.maketext (mainactivity. This,"section"+ Position +"Be clicked", Toast.length_short). Show (); } }); }@NonNull PrivateInfinitescrolllistenerCreateinfinitescrolllistener() {return NewInfinitescrolllistener (Max_items_per_request, LayoutManager) {//Refresh after the first visible item position @Override Public void Onscrolledtoend(Final intfirstvisibleitemposition) {LOG.D ("-->firstvisible","Firstvisibleitemposition:"+firstvisibleitemposition); Simulateloading ();//Load X * 20 Item each intStart = ++page * max_items_per_request;Final Booleanallitemsloaded = Start >= items.size ();if(allitemsloaded) {progressbar.setvisibility (View.gone); }Else{intEnd = start + max_items_per_request;//Synthesis of new data sources Finallist<string> items = getitemstobeloaded (start, end);//re-refresh viewRefreshview (Recyclerview,NewMyadapter (items), firstvisibleitemposition); } } }; }@NonNull PrivateList<string>getitemstobeloaded(intStartintEnd) {list<string> NewItems = items.sublist (start, end);FinalList<string> OldItems = ((myadapter) Recyclerview.getadapter ()). GetItems ();Finallist<string> items =NewLinkedlist<> (); Items.addall (OldItems); Items.addall (NewItems);returnItems }/** * warning! This method was only for demo purposes! * Don ' t do anything like that in your regular project! */ Private void simulateloading() {NewAsynctask<void, Void, void> () {@Override protected void OnPreExecute() {progressbar.setvisibility (view.visible); }@Override protectedVoidDoinbackground(Void ... params) {Try{Thread.Sleep (Simulated_loading_time_in_ms); }Catch(Interruptedexception e) {LOG.E ("Mainactivity", E.getmessage ()); }return NULL; }@Override protected void OnPostExecute(Void param) {progressbar.setvisibility (View.gone); }}.execute (); }}
The process is this, the first to do a fake data, and then pull to the end of the refresh, and then pull to the end of the refresh and then refresh to the maximum value will not do the increased operation, but the judgment is always there
Is evidence that doesn't refresh but is still judging
So let's take a closer look at how this pull-down judgment is going to be achieved.
//继承于RecyclerView.OnScrollListener,也就有了一个很重要的方法// onScrolled(RecyclerView recyclerView, int dx, int dy)publicabstractclass InfiniteScrollListener extends RecyclerView.OnScrollListener
//constructor passed 2 parameters, one is the maximum value to be loaded, There is also a Linearlayoutmanager object, and then do exception handling, empty what the public infinitescrolllistener (int maxitemsperrequest, Linearlayoutmanager LayoutManager) {preconditions.checkifpositive (maxitemsperrequest, "maxitemsperrequest <= 0" ) ; Preconditions.checknotnull (LayoutManager, "LayoutManager = = null" ); this . maxitemsperrequest = maxitemsperrequest; this . LayoutManager = LayoutManager; }
//where to do things and callbacks, The position @Override void onscrolled (Recyclerview Recyclerview, int DX, int dy) {super . onscrolled (recyclerview, DX, dy); //this side is to determine whether it can be added to the if (Canloadmoreitems ()) {Onscrolledtoend (layoutmanager.findfirstvisibleitemposition ()); } }
//加载完刷新的操作,然后再移到那个标志位的位置上去,这也就是为什么每次我们刷新完没有跳到最上面的原因protectedvoidrefreshViewint position) { view.setAdapter(adapter); view.invalidate(); view.scrollToPosition(position); }
//抽象方法了,给我们实现用 publicabstractvoidonScrolledToEnd(finalint firstVisibleItemPosition);
The overall implementation is not too difficult, but there are very good ideas, so recommend to everyone.
Source Address: Https://github.com/ddwhan0123/BlogSample/blob/master/InfiniteScroll-master.zip
Git address: https://github.com/ddwhan0123/InfiniteScroll#download
Turn the git---to pull down to the bottom to refresh Recycleview infinitescroll