You can do a Endless scroll in ListView or RecyclerView with simple steps, with a listener for do request to your web service.
You can do a Endless scroll in ListView or RecyclerView with simple steps, with a listener for do request to your web service.
use git (sourcetree or others) Remember Download the example for understand better the process
git clone https://github.com/fabian7593/InfiniteScroll.git
Download from Here
Another type download by Bintray from
If you need an endless scroll for load data to pagination web service, this is your solution. You need to download the project or import in your gradle, like this.
repositories {
jcenter()
}
dependencies {
compile 'github.frosquivel:infinitescroll:1.0.0'
}
If you have any problem with this dependence, because the library override any styles, colors or others, please change the last line for this code:
compile('github.frosquivel:infinitescroll:1.0.0') {
transitive = false;
}
You need to import the library
import github.frosquivel.infinitescroll.Adapter.InfiniteScrollAdapter;
import github.frosquivel.infinitescroll.Interface.InfiniteScrollImpl;
import github.frosquivel.infinitescroll.Interface.InfiniteScrollInterface;
import github.frosquivel.infinitescroll.Logic.InfiniteListOnScrollListener;
import github.frosquivel.infinitescroll.Model.InfiniteScrollBuilder;
import github.frosquivel.infinitescroll.Model.InfiniteScrollObject;
import github.frosquivel.infinitescroll.Interface.InfiniteScrollInterface;
To start with ScrollInfinite you need a simple ListView or RecyclerView. For example, set this in activity_layout.xml like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
When you have the List view and your respective union in .java Activity, you need to set the listener of Infinite Scroll.
For instance the InfiniteScrollListener, you need ALWAYS instance first the InfiniteScrollObject, because is a constructor parameter of InfiniteScrollListener.
The InfiniteScrollObject has tree params to declare, in a builder pattern.
See the example of this implementation:
InfiniteScrollObject infiniteScrollObject = new InfiniteScrollBuilder(activity)
.setProgressBar(yourProgressBar)
.setCurrentPage(1)
.setMinimunNumberRowLoadingMore(5)
.build();
You need to instance InfiniteListOnScrollListener or InfiniteRecyclerOnScrollListener. And set this into onScrollListener, of the ListView or RecyclerView. This methods implements the Override events of “onLoadMoreData”, and return the next page of request (increment automatically), and the total item count. You need to return the int of the number of response objects of the web services.
Example ListView
listView.setOnScrollListener(new InfiniteListOnScrollListener(infiniteScrollObject) {
@Override
public int onLoadMoreData(int page, int totalItemsCount, ListView view) {
//in this part you can entry the code for request the web service with the new number of page (if have pagination)
//this request need to return a number of response object of the web service
return resquestAPIMethod(page);
}
});
Example RecyclerView You need to send the recycler view, the activity and the infiniteScrollObject
recyclerView.addOnScrollListener(new InfiniteRecyclerOnScrollListener(recyclerView, activity, infiniteScrollObject) {
@Override
public int onLoadMore(int currentPage, int totalItemsCount) {
return resquestAPIMethod(currentPage);
}
});
You need to set the InfiniteScrollInterface, for call the events of onSucess or onFailure. You have the possibillity of implements “InfiniteScrollInterface” and import the required methods, or create an instance of this Interface and usage how that you need.
Example:
InfiniteScrollInterface interfaceInfinite = new InfiniteScrollImpl() {
//If the request is success and get data, call onSuccess event of this interface
//The parameter is an object and you cast this object to another class type, that you need
@Override
public void onSuccess(Object responseModel) {
//set the response model in a static variable
responseModelStatic = (ResponseModel) responseModel;
//add into your adapter list of your recylcer view or list view, the new values response of web service
//And refresh the listView, like this
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
try{
adapter.notifyDataSetChanged();
}catch(Exception ex){}
}
});
}
//If the request have a failure, call this event
@Override
public void onFailure(String errorResponse){
Toast.makeText(activity, errorResponse, Toast.LENGTH_SHORT).show();
}
};
When you call the library for request you web service, you can do use, OkHttp, Retrofit, Volley, Async task etc… when this class obtain the new data of response or get a failure you can need to call the methods of interface like this:
//instance the request and ok http client
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(url)
.build();
//do the request
client.newCall(request)
.enqueue(new Callback() {
@Override
public void onFailure(final Call call, IOException e) {
infiniteScrollInterface.onFailure(stackTrace);
}
@Override
public void onResponse(Call call, final Response response) throws IOException {
infiniteScrollInterface.onSuccess(responseModel);
}
});
You can view the current example in the code, if you download or clone the repository, in app folder you can view all code. In this example the real important classes are:
We use the “CountryApi” for example of pagination web service, you can see this in: https://github.com/fabian7593/CountryAPI
All the code has a internal documentation for more explanation of this example.
Pay attention to how the scroll bar increases.
The progress bar is not shown because the variable of MinimunNumberRowLoadingMore has a high value (5), to prevent the user from noticing when more data is loaded.
Feel free to contact me to add yours apps to this list.
Infinite Scroll was created to make Android Devoloper’s life easy. If you have any feedback please let us know in the issues by creating an issue with this format:
Suggestions about how to improve the library or new features are welcome. Thanks for choosing us.
We obtain references in:
https://github.com/codepath/android_guides/wiki/Endless-Scrolling-with-AdapterViews-and-RecyclerView
We use the “CountryApi” for example of pagination web service, you can see this in:
https://github.com/fabian7593/CountryAPI
Author
Infinite Scroll author…
The goal for InfiniteScroll is to allow Android Developers care about what is important, features not getting worry about something that should be trivial such as do endless scroll. There are many features and other issues waiting. If you would like to contribute please reach to us, or maybe be bold! Getting a surprise pull request is very gratifying.
Copyright 2017 Fabian Rosales
Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an “AS IS” BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.