From 9c40031300d4aa6154f720c9988de4d5dcc44bac Mon Sep 17 00:00:00 2001 From: tibbi Date: Wed, 13 Jul 2016 22:51:38 +0200 Subject: [PATCH] implement Pull to refresh --- .../filemanager/fragments/ItemsFragment.java | 22 ++++++++++++++++--- app/src/main/res/layout/items_fragment.xml | 15 ++++++++----- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/com/simplemobiletools/filemanager/fragments/ItemsFragment.java b/app/src/main/java/com/simplemobiletools/filemanager/fragments/ItemsFragment.java index 07b1d17a..ba7f94c1 100644 --- a/app/src/main/java/com/simplemobiletools/filemanager/fragments/ItemsFragment.java +++ b/app/src/main/java/com/simplemobiletools/filemanager/fragments/ItemsFragment.java @@ -5,6 +5,7 @@ import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.support.annotation.Nullable; +import android.support.v4.widget.SwipeRefreshLayout; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -27,8 +28,10 @@ import java.util.List; import butterknife.BindView; import butterknife.ButterKnife; -public class ItemsFragment extends android.support.v4.app.Fragment implements AdapterView.OnItemClickListener { +public class ItemsFragment extends android.support.v4.app.Fragment + implements AdapterView.OnItemClickListener, SwipeRefreshLayout.OnRefreshListener { @BindView(R.id.items_list) ListView mListView; + @BindView(R.id.items_swipe_refresh) SwipeRefreshLayout mSwipeRefreshLayout; private List mItems; private ItemInteractionListener mListener; @@ -46,7 +49,9 @@ public class ItemsFragment extends android.support.v4.app.Fragment implements Ad public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); mShowHidden = Config.newInstance(getContext()).getShowHidden(); + mItems = new ArrayList<>(); fillItems(); + mSwipeRefreshLayout.setOnRefreshListener(this); } @Override @@ -60,8 +65,13 @@ public class ItemsFragment extends android.support.v4.app.Fragment implements Ad private void fillItems() { final String path = getArguments().getString(Constants.PATH); - mItems = getItems(path); - Collections.sort(mItems); + final List newItems = getItems(path); + Collections.sort(newItems); + if (mItems != null && newItems.toString().equals(mItems.toString())) { + return; + } + + mItems = newItems; final ItemsAdapter adapter = new ItemsAdapter(getContext(), mItems); mListView.setAdapter(adapter); @@ -108,6 +118,12 @@ public class ItemsFragment extends android.support.v4.app.Fragment implements Ad } } + @Override + public void onRefresh() { + fillItems(); + mSwipeRefreshLayout.setRefreshing(false); + } + public interface ItemInteractionListener { void itemClicked(String path); } diff --git a/app/src/main/res/layout/items_fragment.xml b/app/src/main/res/layout/items_fragment.xml index dfd750ef..afe2e9fa 100644 --- a/app/src/main/res/layout/items_fragment.xml +++ b/app/src/main/res/layout/items_fragment.xml @@ -5,11 +5,16 @@ android:layout_width="match_parent" android:layout_height="match_parent"> - + android:layout_height="wrap_content"> + +