From c610364ef772ad230232252402c4f067a1fdbd45 Mon Sep 17 00:00:00 2001 From: jendib Date: Sat, 22 Nov 2014 22:43:31 +0100 Subject: [PATCH] Android: Infinite scrolling --- .../com/sismics/docs/MainApplication.java | 1 - .../sismics/docs/adapter/DocListAdapter.java | 15 ++++- .../docs/fragment/DocListFragment.java | 59 ++++++++++++++----- .../docs/resource/DocumentResource.java | 5 +- 4 files changed, 59 insertions(+), 21 deletions(-) diff --git a/docs-android/app/src/main/java/com/sismics/docs/MainApplication.java b/docs-android/app/src/main/java/com/sismics/docs/MainApplication.java index 93caf893..9e354e37 100644 --- a/docs-android/app/src/main/java/com/sismics/docs/MainApplication.java +++ b/docs-android/app/src/main/java/com/sismics/docs/MainApplication.java @@ -23,7 +23,6 @@ public class MainApplication extends Application { // TODO Fullscreen preview // TODO Sharing // TODO Error feedback - // TODO Infinite scrolling on documents // TODO Searching // TODO Printing diff --git a/docs-android/app/src/main/java/com/sismics/docs/adapter/DocListAdapter.java b/docs-android/app/src/main/java/com/sismics/docs/adapter/DocListAdapter.java index f5c69ab9..cc4aa19c 100644 --- a/docs-android/app/src/main/java/com/sismics/docs/adapter/DocListAdapter.java +++ b/docs-android/app/src/main/java/com/sismics/docs/adapter/DocListAdapter.java @@ -89,11 +89,20 @@ public class DocListAdapter extends RecyclerView.Adapter previousTotal) { + loading = false; + previousTotal = totalItemCount; + } + } + if (!loading && totalItemCount - visibleItemCount <= firstVisibleItem + 3) { + loadDocuments(); + loading = true; + } + } + }); + // Grab the documents - refreshDocuments(); + loadDocuments(); return view; } @@ -65,12 +95,11 @@ public class DocListFragment extends Fragment { /** * Refresh the document list. */ - private void refreshDocuments() { - DocumentResource.list(getActivity(), new JsonHttpResponseHandler() { + private void loadDocuments() { + DocumentResource.list(getActivity(), adapter.getItemCount(), new JsonHttpResponseHandler() { @Override public void onSuccess(int statusCode, Header[] headers, JSONObject response) { - adapter.setDocuments(response.optJSONArray("documents")); - + adapter.addDocuments(response.optJSONArray("documents"), false); if (getView() != null) { getView().findViewById(R.id.progressBar).setVisibility(View.GONE); } diff --git a/docs-android/app/src/main/java/com/sismics/docs/resource/DocumentResource.java b/docs-android/app/src/main/java/com/sismics/docs/resource/DocumentResource.java index d6680d14..a8d575db 100644 --- a/docs-android/app/src/main/java/com/sismics/docs/resource/DocumentResource.java +++ b/docs-android/app/src/main/java/com/sismics/docs/resource/DocumentResource.java @@ -17,11 +17,12 @@ public class DocumentResource extends BaseResource { * @param context Context * @param responseHandler Callback */ - public static void list(Context context, JsonHttpResponseHandler responseHandler) { + public static void list(Context context, int offset, JsonHttpResponseHandler responseHandler) { init(context); RequestParams params = new RequestParams(); - params.put("limit", 50); + params.put("limit", 20); + params.put("offset", offset); params.put("sort_column", 3); params.put("asc", false); client.get(getApiUrl(context) + "/document/list", params, responseHandler);