Android: Infinite scrolling

This commit is contained in:
jendib 2014-11-22 22:43:31 +01:00
parent bc719b3165
commit c610364ef7
4 changed files with 59 additions and 21 deletions

View File

@ -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

View File

@ -89,11 +89,20 @@ public class DocListAdapter extends RecyclerView.Adapter<DocListAdapter.ViewHold
}
/**
* Update the displayed documents.
* Add documents to display.
*
* @param documents Documents
* @param reset Reset the list
*/
public void setDocuments(JSONArray documents) {
this.documents = documents;
public void addDocuments(JSONArray documents, boolean reset) {
if (this.documents == null || reset) {
this.documents = new JSONArray();
}
for (int i = 0; i < documents.length(); i++) {
this.documents.put(documents.optJSONObject(i));
}
notifyDataSetChanged();
}
}

View File

@ -24,26 +24,33 @@ import org.json.JSONObject;
* @author bgamard.
*/
public class DocListFragment extends Fragment {
/**
* Documents adapter.
*/
DocListAdapter adapter;
// Infinite scrolling things
private boolean loading = true;
private int previousTotal = 0;
int firstVisibleItem, visibleItemCount, totalItemCount;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Initialize the view
View view = inflater.inflate(R.layout.doc_list_fragment, container, false);
// Configure the RecyclerView
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.docList);
recyclerView.setHasFixedSize(true);
recyclerView.setLongClickable(true);
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
adapter = new DocListAdapter();
recyclerView.setAdapter(adapter);
recyclerView.setHasFixedSize(true);
recyclerView.setLongClickable(true);
recyclerView.addItemDecoration(new DividerItemDecoration(getResources().getDrawable(R.drawable.abc_list_divider_mtrl_alpha)));
// Configure the LayoutManager
final LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
// Document opening
recyclerView.addOnItemTouchListener(new RecyclerItemClickListener(getActivity(), new RecyclerItemClickListener.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
@ -56,8 +63,31 @@ public class DocListFragment extends Fragment {
}
}));
// Infinite scrolling
recyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
visibleItemCount = recyclerView.getChildCount();
totalItemCount = layoutManager.getItemCount();
firstVisibleItem = layoutManager.findFirstVisibleItemPosition();
if (loading) {
if (totalItemCount > 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);
}

View File

@ -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);