[Android] Update only needed views

Geoffrey Métais git at videolan.org
Tue Nov 29 12:04:50 CET 2016


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Mon Nov 28 16:08:01 2016 +0100| [480e8804864ad612912cd1a579de369720a156ab] | committer: Geoffrey Métais

Update only needed views

> https://code.videolan.org/videolan/vlc-android/commit/480e8804864ad612912cd1a579de369720a156ab
---

 .../org/videolan/vlc/gui/browser/BaseBrowserAdapter.java | 16 +++++++++++++---
 .../src/org/videolan/vlc/gui/video/VideoListAdapter.java | 15 ++++++++++++++-
 2 files changed, 27 insertions(+), 4 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserAdapter.java b/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserAdapter.java
index af23eff..361c4ea 100644
--- a/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserAdapter.java
+++ b/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserAdapter.java
@@ -26,6 +26,7 @@ import android.databinding.DataBindingUtil;
 import android.databinding.ViewDataBinding;
 import android.graphics.BitmapFactory;
 import android.graphics.drawable.BitmapDrawable;
+import android.support.v4.util.SparseArrayCompat;
 import android.support.v7.widget.RecyclerView;
 import android.text.TextUtils;
 import android.util.Log;
@@ -44,10 +45,10 @@ import org.videolan.vlc.VLCApplication;
 import org.videolan.vlc.databinding.BrowserItemSeparatorBinding;
 import org.videolan.vlc.databinding.DirectoryViewItemBinding;
 import org.videolan.vlc.gui.helpers.UiTools;
-import org.videolan.vlc.media.MediaDatabase;
 import org.videolan.vlc.media.MediaUtils;
 import org.videolan.vlc.util.MediaItemFilter;
 
+import java.lang.ref.WeakReference;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.LinkedList;
@@ -69,7 +70,7 @@ public class BaseBrowserAdapter extends RecyclerView.Adapter<BaseBrowserAdapter.
     ArrayList<MediaLibraryItem> mMediaList = new ArrayList<>();
     ArrayList<MediaLibraryItem> mOriginalData = null;
     BaseBrowserFragment fragment;
-    MediaDatabase mDbManager;
+    SparseArrayCompat<WeakReference<MediaViewHolder>> mHolders = new SparseArrayCompat<>();
     private int mTop = 0;
     private int mMediaCount = 0;
     private ItemFilter mFilter = new ItemFilter();
@@ -108,6 +109,7 @@ public class BaseBrowserAdapter extends RecyclerView.Adapter<BaseBrowserAdapter.
     }
 
     private void onBindMediaViewHolder(final MediaViewHolder vh, int position) {
+        mHolders.put(position, new WeakReference<>(vh));
         final MediaWrapper media = (MediaWrapper) getItem(position);
         vh.binding.setItem(media);
         vh.binding.setHasContextMenu(true);
@@ -118,6 +120,12 @@ public class BaseBrowserAdapter extends RecyclerView.Adapter<BaseBrowserAdapter.
         vh.setViewBackground(vh.itemView.hasFocus(), mSelectedItems.contains(position));
     }
 
+
+    @Override
+    public void onViewRecycled(ViewHolder holder) {
+        mHolders.remove(holder.getAdapterPosition());
+    }
+
     @Override
     public int getItemCount() {
         return mMediaList.size();
@@ -288,7 +296,9 @@ public class BaseBrowserAdapter extends RecyclerView.Adapter<BaseBrowserAdapter.
             ((Storage) item).setDescription(description);
         else
             return;
-        notifyItemChanged(position);
+        WeakReference<MediaViewHolder> wr = mHolders.get(position);
+        if (wr != null && wr.get() != null)
+            wr.get().binding.setItem((MediaLibraryItem) item);
     }
 
     public void addAll(ArrayList<MediaWrapper> mediaList){
diff --git a/vlc-android/src/org/videolan/vlc/gui/video/VideoListAdapter.java b/vlc-android/src/org/videolan/vlc/gui/video/VideoListAdapter.java
index 4ab55d7..2a2da2f 100644
--- a/vlc-android/src/org/videolan/vlc/gui/video/VideoListAdapter.java
+++ b/vlc-android/src/org/videolan/vlc/gui/video/VideoListAdapter.java
@@ -31,6 +31,7 @@ import android.support.annotation.MainThread;
 import android.support.annotation.Nullable;
 import android.support.v4.content.ContextCompat;
 import android.support.v4.util.SimpleArrayMap;
+import android.support.v4.util.SparseArrayCompat;
 import android.support.v7.util.SortedList;
 import android.support.v7.widget.GridLayoutManager;
 import android.support.v7.widget.RecyclerView;
@@ -55,6 +56,7 @@ import org.videolan.vlc.media.MediaUtils;
 import org.videolan.vlc.util.MediaItemFilter;
 import org.videolan.vlc.util.Strings;
 
+import java.lang.ref.WeakReference;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.LinkedList;
@@ -73,6 +75,7 @@ public class VideoListAdapter extends RecyclerView.Adapter<VideoListAdapter.View
 
     public final static int SORT_BY_DATE = 2;
     private boolean mListMode = false, mActionMode;
+    SparseArrayCompat<WeakReference<ViewHolder>> mHolders = new SparseArrayCompat<>();
     private List<Integer> mSelectedItems = new LinkedList<>();
     private VideoGridFragment mFragment;
     private VideoComparator mVideoComparator = new VideoComparator();
@@ -110,6 +113,13 @@ public class VideoListAdapter extends RecyclerView.Adapter<VideoListAdapter.View
         boolean isSelected = mActionMode && mSelectedItems.contains(position);
         holder.setOverlay(mActionMode && isSelected);
         holder.binding.setVariable(BR.bgColor, ContextCompat.getColor(holder.itemView.getContext(), mListMode && isSelected ? R.color.orange200transparent : R.color.transparent));
+        mHolders.put(position, new WeakReference<>(holder));
+    }
+
+
+    @Override
+    public void onViewRecycled(ViewHolder holder) {
+        mHolders.remove(holder.getAdapterPosition());
     }
 
     @MainThread
@@ -120,7 +130,10 @@ public class VideoListAdapter extends RecyclerView.Adapter<VideoListAdapter.View
             Long time = times.get(media.getId());
             if (time != null && time != media.getTime()) {
                 media.setTime(time);
-                notifyItemChanged(getPosition(media));
+                int position = getPosition(media);
+                WeakReference<ViewHolder> wr = mHolders.get(position);
+                if (wr != null && wr.get() != null)
+                    fillView(wr.get(), media);
             }
         }
     }



More information about the Android mailing list