Android: swipe refresh on documents list

This commit is contained in:
jendib 2014-11-26 01:01:36 +01:00
parent 1d08508e51
commit 824e37b8ea
3 changed files with 39 additions and 12 deletions

View File

@ -20,7 +20,6 @@ public class MainApplication extends Application {
JSONObject json = PreferenceUtil.getCachedJson(getApplicationContext(), PreferenceUtil.PREF_CACHED_USER_INFO_JSON); JSONObject json = PreferenceUtil.getCachedJson(getApplicationContext(), PreferenceUtil.PREF_CACHED_USER_INFO_JSON);
ApplicationContext.getInstance().setUserInfo(getApplicationContext(), json); ApplicationContext.getInstance().setUserInfo(getApplicationContext(), json);
// TODO Documents list page loading feedback
// TODO Fullscreen preview // TODO Fullscreen preview
// TODO Caching preferences // TODO Caching preferences
// TODO Edit sharing // TODO Edit sharing

View File

@ -3,6 +3,7 @@ package com.sismics.docs.fragment;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView; import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater; import android.view.LayoutInflater;
@ -30,11 +31,6 @@ import de.greenrobot.event.EventBus;
* @author bgamard. * @author bgamard.
*/ */
public class DocListFragment extends Fragment { public class DocListFragment extends Fragment {
/**
* Recycler view.
*/
private EmptyRecyclerView recyclerView;
/** /**
* Documents adapter. * Documents adapter.
*/ */
@ -45,13 +41,17 @@ public class DocListFragment extends Fragment {
*/ */
private String query; private String query;
// View cache
private EmptyRecyclerView recyclerView;
private SwipeRefreshLayout swipeRefreshLayout;
// Infinite scrolling things // Infinite scrolling things
private boolean loading = true; private boolean loading = true;
private int previousTotal = 0; private int previousTotal = 0;
@Override @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.doc_list_fragment, container, false); final View view = inflater.inflate(R.layout.doc_list_fragment, container, false);
// Configure the RecyclerView // Configure the RecyclerView
recyclerView = (EmptyRecyclerView) view.findViewById(R.id.docList); recyclerView = (EmptyRecyclerView) view.findViewById(R.id.docList);
@ -65,6 +65,19 @@ public class DocListFragment extends Fragment {
final LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity()); final LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager); recyclerView.setLayoutManager(layoutManager);
// Configure the swipe refresh layout
swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipeRefreshLayout);
swipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_bright,
android.R.color.holo_green_light,
android.R.color.holo_orange_light,
android.R.color.holo_red_light);
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
loadDocuments(view, true);
}
});
// Document opening // Document opening
recyclerView.addOnItemTouchListener(new RecyclerItemClickListener(getActivity(), new RecyclerItemClickListener.OnItemClickListener() { recyclerView.addOnItemTouchListener(new RecyclerItemClickListener(getActivity(), new RecyclerItemClickListener.OnItemClickListener() {
@Override @Override
@ -139,7 +152,10 @@ public class DocListFragment extends Fragment {
loading = true; loading = true;
previousTotal = 0; previousTotal = 0;
adapter.clearDocuments(); adapter.clearDocuments();
} else {
swipeRefreshLayout.setRefreshing(true);
} }
recyclerView.setEmptyView(progressBar); recyclerView.setEmptyView(progressBar);
DocumentResource.list(getActivity(), reset ? 0 : adapter.getItemCount(), query, new JsonHttpResponseHandler() { DocumentResource.list(getActivity(), reset ? 0 : adapter.getItemCount(), query, new JsonHttpResponseHandler() {
@ -160,6 +176,11 @@ public class DocListFragment extends Fragment {
Toast.makeText(getActivity(), R.string.error_loading_documents, Toast.LENGTH_SHORT).show(); Toast.makeText(getActivity(), R.string.error_loading_documents, Toast.LENGTH_SHORT).show();
} }
} }
@Override
public void onFinish() {
swipeRefreshLayout.setRefreshing(false);
}
}); });
} }
} }

View File

@ -4,6 +4,11 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent"> android:layout_height="match_parent">
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipeRefreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.sismics.docs.ui.view.EmptyRecyclerView <com.sismics.docs.ui.view.EmptyRecyclerView
android:id="@+id/docList" android:id="@+id/docList"
android:layout_width="match_parent" android:layout_width="match_parent"
@ -11,6 +16,8 @@
android:scrollbars="vertical"> android:scrollbars="vertical">
</com.sismics.docs.ui.view.EmptyRecyclerView> </com.sismics.docs.ui.view.EmptyRecyclerView>
</android.support.v4.widget.SwipeRefreshLayout>
<ProgressBar <ProgressBar
android:id="@+id/progressBar" android:id="@+id/progressBar"
style="?android:attr/progressBarStyleLarge" style="?android:attr/progressBarStyleLarge"