Android- Pagination for Algolia/Recyclerviews Searches
Algolia a popular SaaS , that provides a service to create a Search API easily and has lightning fast responses is quickly growing and catching up.
Recycler View on the other hand is a concept that helps dynamically create a list from APIs.
To view on recycler view you can see my previous article here
The API part in the previous library used Volley , here we wont be using that as Algolia has it’s own methods to take care of the response.
Like the previous article we create a res file with a Recycler View as below and call the res file activity_main
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android" >
<android.support.v7.widget.RecyclerView
android:id="@+id/my_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical" />
</LinearLayout>
Now in the MainActivity Classs we will initialise the recycler views. We will be using a GridLayoutManager too !
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private GridLayoutManager gridLayoutManager;
If you are using Algolia then initialise the following too ! But if you are looking for only recycler view and pagination you need not add the following !
Client client;
Index index;
int total_pages;
int nbPage;
Client and Index come from Algolia SDK. You can get the sdk by putting the following under dependencies under the build.gradle of your app
dependencies {
compile 'com.algolia:algoliasearch-android:3.+@aar'
}Inside the onCreate Method initialize your RecyclerView , Adapters etc
mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
gridLayoutManager = new GridLayoutManager(this,3);
mAdapter = new movieadapter(movies);
mRecyclerView.setAdapter(mAdapter);
mRecyclerView.setLayoutManager(gridLayoutManager);
mRecyclerView.addOnScrollListener(new EndlessRecyclerViewScrollListener(gridLayoutManager) {
@Override
public void onLoadMore(int page, int totalItemsCount) {
if(isThereMore())
{
LoadMoreDataFromApi(nbPage);
}
else
{
Log.e("No more ","data");
}
}
});
You can get the EndlessRecyclerViewScrollListener here
If you are using Algolia you might want to initialise your client and index !
Client client = new Client("YOUR_APP_ID", "YOUR_API_KEY");
index = client.initIndex("index name");Create function called LoadMoreDataFromApi
public void LoadMoreDataFromApi(int page)
{
Query query = new Query("")
query.setPage(page);
index.searchAsync(query, new CompletionHandler() {
@Override
public void requestCompleted(JSONObject content, AlgoliaException error) {
try {
JSONArray jsonArray = content.getJSONArray("hits");
//Parse the JSON and add in a ArrayList of type your //model
nbPage++;//update the current page
mAdapter.notifyItemChanged(nbPage);
}
catch (Exception e)
{
e.printStackTrace();
}
// [...]
}
});
}
Write function called isThereMore() that returns true if more pages are there or false when no more content is there .