mirror of
https://github.com/sismics/docs.git
synced 2024-11-22 22:07:56 +01:00
Android: Infinite scrolling
This commit is contained in:
parent
bc719b3165
commit
c610364ef7
@ -23,7 +23,6 @@ public class MainApplication extends Application {
|
|||||||
// TODO Fullscreen preview
|
// TODO Fullscreen preview
|
||||||
// TODO Sharing
|
// TODO Sharing
|
||||||
// TODO Error feedback
|
// TODO Error feedback
|
||||||
// TODO Infinite scrolling on documents
|
|
||||||
// TODO Searching
|
// TODO Searching
|
||||||
// TODO Printing
|
// TODO Printing
|
||||||
|
|
||||||
|
@ -89,11 +89,20 @@ public class DocListAdapter extends RecyclerView.Adapter<DocListAdapter.ViewHold
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Update the displayed documents.
|
* Add documents to display.
|
||||||
|
*
|
||||||
* @param documents Documents
|
* @param documents Documents
|
||||||
|
* @param reset Reset the list
|
||||||
*/
|
*/
|
||||||
public void setDocuments(JSONArray documents) {
|
public void addDocuments(JSONArray documents, boolean reset) {
|
||||||
this.documents = documents;
|
if (this.documents == null || reset) {
|
||||||
|
this.documents = new JSONArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < documents.length(); i++) {
|
||||||
|
this.documents.put(documents.optJSONObject(i));
|
||||||
|
}
|
||||||
|
|
||||||
notifyDataSetChanged();
|
notifyDataSetChanged();
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -24,26 +24,33 @@ import org.json.JSONObject;
|
|||||||
* @author bgamard.
|
* @author bgamard.
|
||||||
*/
|
*/
|
||||||
public class DocListFragment extends Fragment {
|
public class DocListFragment extends Fragment {
|
||||||
|
/**
|
||||||
|
* Documents adapter.
|
||||||
|
*/
|
||||||
DocListAdapter adapter;
|
DocListAdapter adapter;
|
||||||
|
|
||||||
|
// Infinite scrolling things
|
||||||
|
private boolean loading = true;
|
||||||
|
private int previousTotal = 0;
|
||||||
|
int firstVisibleItem, visibleItemCount, totalItemCount;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
||||||
// Initialize the view
|
|
||||||
View view = inflater.inflate(R.layout.doc_list_fragment, container, false);
|
View view = inflater.inflate(R.layout.doc_list_fragment, container, false);
|
||||||
|
|
||||||
|
// Configure the RecyclerView
|
||||||
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.docList);
|
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();
|
adapter = new DocListAdapter();
|
||||||
recyclerView.setAdapter(adapter);
|
recyclerView.setAdapter(adapter);
|
||||||
|
recyclerView.setHasFixedSize(true);
|
||||||
|
recyclerView.setLongClickable(true);
|
||||||
recyclerView.addItemDecoration(new DividerItemDecoration(getResources().getDrawable(R.drawable.abc_list_divider_mtrl_alpha)));
|
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() {
|
recyclerView.addOnItemTouchListener(new RecyclerItemClickListener(getActivity(), new RecyclerItemClickListener.OnItemClickListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onItemClick(View view, int position) {
|
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
|
// Grab the documents
|
||||||
refreshDocuments();
|
loadDocuments();
|
||||||
|
|
||||||
return view;
|
return view;
|
||||||
}
|
}
|
||||||
@ -65,12 +95,11 @@ public class DocListFragment extends Fragment {
|
|||||||
/**
|
/**
|
||||||
* Refresh the document list.
|
* Refresh the document list.
|
||||||
*/
|
*/
|
||||||
private void refreshDocuments() {
|
private void loadDocuments() {
|
||||||
DocumentResource.list(getActivity(), new JsonHttpResponseHandler() {
|
DocumentResource.list(getActivity(), adapter.getItemCount(), new JsonHttpResponseHandler() {
|
||||||
@Override
|
@Override
|
||||||
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
|
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
|
||||||
adapter.setDocuments(response.optJSONArray("documents"));
|
adapter.addDocuments(response.optJSONArray("documents"), false);
|
||||||
|
|
||||||
if (getView() != null) {
|
if (getView() != null) {
|
||||||
getView().findViewById(R.id.progressBar).setVisibility(View.GONE);
|
getView().findViewById(R.id.progressBar).setVisibility(View.GONE);
|
||||||
}
|
}
|
||||||
|
@ -17,11 +17,12 @@ public class DocumentResource extends BaseResource {
|
|||||||
* @param context Context
|
* @param context Context
|
||||||
* @param responseHandler Callback
|
* @param responseHandler Callback
|
||||||
*/
|
*/
|
||||||
public static void list(Context context, JsonHttpResponseHandler responseHandler) {
|
public static void list(Context context, int offset, JsonHttpResponseHandler responseHandler) {
|
||||||
init(context);
|
init(context);
|
||||||
|
|
||||||
RequestParams params = new RequestParams();
|
RequestParams params = new RequestParams();
|
||||||
params.put("limit", 50);
|
params.put("limit", 20);
|
||||||
|
params.put("offset", offset);
|
||||||
params.put("sort_column", 3);
|
params.put("sort_column", 3);
|
||||||
params.put("asc", false);
|
params.put("asc", false);
|
||||||
client.get(getApiUrl(context) + "/document/list", params, responseHandler);
|
client.get(getApiUrl(context) + "/document/list", params, responseHandler);
|
||||||
|
Loading…
Reference in New Issue
Block a user