[Android] Fix adapters with holder handling click events

Geoffrey Métais git at videolan.org
Fri Dec 18 11:01:49 CET 2015


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Fri Dec 18 11:01:07 2015 +0100| [6aac814a421dcdc03f6e14618e828af0187c6932] | committer: Geoffrey Métais

Fix adapters with holder handling click events

> https://code.videolan.org/videolan/vlc-android/commit/6aac814a421dcdc03f6e14618e828af0187c6932
---

 .../vlc/gui/browser/BaseBrowserAdapter.java        | 64 +++++++++++++---------
 .../vlc/gui/browser/FilePickerAdapter.java         |  4 +-
 .../vlc/gui/browser/StorageBrowserAdapter.java     |  8 +--
 .../videolan/vlc/gui/video/VideoListAdapter.java   | 21 ++++---
 4 files changed, 52 insertions(+), 45 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 e3930c9..41b26f8 100644
--- a/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserAdapter.java
+++ b/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserAdapter.java
@@ -148,34 +148,19 @@ public class BaseBrowserAdapter extends  RecyclerView.Adapter<RecyclerView.ViewH
         }
 
         public void onClick(View v){
-            final MediaWrapper mw = (MediaWrapper) getItem(getAdapterPosition());
-            mw.removeFlags(MediaWrapper.MEDIA_FORCE_AUDIO);
-
-            if (mw.getType() == MediaWrapper.TYPE_DIR)
-                fragment.browse(mw, getAdapterPosition(), true);
-            else if (mw.getType() == MediaWrapper.TYPE_VIDEO)
-                MediaUtils.openMedia(v.getContext(), mw);
-            else  if (mw.getType() == MediaWrapper.TYPE_AUDIO) {
-                int position = 0;
-                LinkedList<MediaWrapper> mediaLocations = new LinkedList<MediaWrapper>();
-                MediaWrapper mediaItem;
-                for (Object item : mMediaList)
-                    if (item instanceof MediaWrapper) {
-                        mediaItem = (MediaWrapper) item;
-                        if (mediaItem.getType() == MediaWrapper.TYPE_VIDEO || mediaItem.getType() == MediaWrapper.TYPE_AUDIO) {
-                            mediaLocations.add(mediaItem);
-                            if (mediaItem.equals(mw))
-                                position = mediaLocations.size() - 1;
-                        }
-                    }
-                MediaUtils.openList(itemView.getContext(), mediaLocations, position);
-            } else {
-                MediaUtils.openStream(itemView.getContext(), mw.getLocation());
-            }
+            openMediaFromView(this, v);
+        }
+
+        protected void openStorage() {
+            MediaWrapper mw = new MediaWrapper(((Storage) getItem(getAdapterPosition())).getUri());
+            mw.setType(MediaWrapper.TYPE_DIR);
+            fragment.browse(mw, getAdapterPosition(), checkBox.isChecked());
         }
 
         public void onCheckBoxClick(View v){
-            checkBoxAction(v);
+            if (getItem(getAdapterPosition()) instanceof Storage) {
+                checkBoxAction(v, ((Storage) getItem(getAdapterPosition())).getUri().getPath());
+            }
         }
 
         public void onMoreClick(View v){
@@ -361,5 +346,32 @@ public class BaseBrowserAdapter extends  RecyclerView.Adapter<RecyclerView.ViewH
             return null;
     }
 
-    protected void checkBoxAction(View v){}
+    protected void checkBoxAction(View v, String path){}
+
+    protected void openMediaFromView(MediaViewHolder holder, View v) {
+        final MediaWrapper mw = (MediaWrapper) getItem(holder.getAdapterPosition());
+        mw.removeFlags(MediaWrapper.MEDIA_FORCE_AUDIO);
+
+        if (mw.getType() == MediaWrapper.TYPE_DIR)
+            fragment.browse(mw, holder.getAdapterPosition(), true);
+        else if (mw.getType() == MediaWrapper.TYPE_VIDEO)
+            MediaUtils.openMedia(holder.itemView.getContext(), mw);
+        else  if (mw.getType() == MediaWrapper.TYPE_AUDIO) {
+            int position = 0;
+            LinkedList<MediaWrapper> mediaLocations = new LinkedList<MediaWrapper>();
+            MediaWrapper mediaItem;
+            for (Object item : mMediaList)
+                if (item instanceof MediaWrapper) {
+                    mediaItem = (MediaWrapper) item;
+                    if (mediaItem.getType() == MediaWrapper.TYPE_VIDEO || mediaItem.getType() == MediaWrapper.TYPE_AUDIO) {
+                        mediaLocations.add(mediaItem);
+                        if (mediaItem.equals(mw))
+                            position = mediaLocations.size() - 1;
+                    }
+                }
+            MediaUtils.openList(holder.itemView.getContext(), mediaLocations, position);
+        } else {
+            MediaUtils.openStream(holder.itemView.getContext(), mw.getLocation());
+        }
+    }
 }
diff --git a/vlc-android/src/org/videolan/vlc/gui/browser/FilePickerAdapter.java b/vlc-android/src/org/videolan/vlc/gui/browser/FilePickerAdapter.java
index a3018ca..49f3bb1 100644
--- a/vlc-android/src/org/videolan/vlc/gui/browser/FilePickerAdapter.java
+++ b/vlc-android/src/org/videolan/vlc/gui/browser/FilePickerAdapter.java
@@ -28,7 +28,6 @@ import android.view.View;
 
 import org.videolan.libvlc.Media;
 import org.videolan.vlc.media.MediaWrapper;
-import org.videolan.vlc.R;
 
 public class FilePickerAdapter extends BaseBrowserAdapter {
 
@@ -58,8 +57,7 @@ public class FilePickerAdapter extends BaseBrowserAdapter {
         return mediaWrapper.getType() == MediaWrapper.TYPE_DIR || mediaWrapper.getType() == MediaWrapper.TYPE_SUBTITLE;
     }
 
-    protected void openMediaFromView(View v) {
-        final MediaViewHolder holder = (MediaViewHolder) v.getTag(R.id.layout_item);
+    protected void openMediaFromView(MediaViewHolder holder, View v) {
         final MediaWrapper media = (MediaWrapper) getItem(holder.getAdapterPosition());
         if (media.getType() == MediaWrapper.TYPE_DIR)
             fragment.browse(media, holder.getAdapterPosition(), true);
diff --git a/vlc-android/src/org/videolan/vlc/gui/browser/StorageBrowserAdapter.java b/vlc-android/src/org/videolan/vlc/gui/browser/StorageBrowserAdapter.java
index 43aaed0..122d824 100644
--- a/vlc-android/src/org/videolan/vlc/gui/browser/StorageBrowserAdapter.java
+++ b/vlc-android/src/org/videolan/vlc/gui/browser/StorageBrowserAdapter.java
@@ -125,7 +125,7 @@ public class StorageBrowserAdapter extends BaseBrowserAdapter {
 //                }
                 //Remove subfolders, it would be redundant
                 for (String customDirPath : mMediaDirsLocation) {
-                    if (customDirPath.startsWith(path+"/"))
+                    if (customDirPath.startsWith(path + "/"))
                         mDbManager.removeDir(customDirPath);
                 }
                 updateMediaDirs();
@@ -143,16 +143,14 @@ public class StorageBrowserAdapter extends BaseBrowserAdapter {
         });
     }
 
-    protected void openMediaFromView(View v) {
-        MediaViewHolder holder = (MediaViewHolder) v.getTag(R.id.layout_item);
+    protected void openMediaFromView(MediaViewHolder holder, View v) {
         MediaWrapper mw = new MediaWrapper(((Storage) getItem(holder.getAdapterPosition())).getUri());
         mw.setType(MediaWrapper.TYPE_DIR);
         fragment.browse(mw, holder.getAdapterPosition(), holder.checkBox.isChecked());
     }
 
-    protected void checkBoxAction(View v){
+    protected void checkBoxAction(View v, String path){
             boolean isChecked = ((CheckBox) v).isChecked();
-            String path = ((Storage) getItem(((Integer)v.getTag()).intValue())).getUri().getPath();
             if (isChecked)
                 addDir(path);
             else
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 b304808..1986328 100644
--- a/vlc-android/src/org/videolan/vlc/gui/video/VideoListAdapter.java
+++ b/vlc-android/src/org/videolan/vlc/gui/video/VideoListAdapter.java
@@ -53,7 +53,7 @@ import java.util.Comparator;
 import java.util.Locale;
 
 public class VideoListAdapter extends RecyclerView.Adapter<VideoListAdapter.ViewHolder>
-        implements Comparator<MediaWrapper>, View.OnLongClickListener {
+        implements Comparator<MediaWrapper> {
 
     public final static String TAG = "VLC/VideoListAdapter";
 
@@ -107,7 +107,6 @@ public class VideoListAdapter extends RecyclerView.Adapter<VideoListAdapter.View
 
         holder.binding.setVariable(BR.media, media);
         holder.binding.executePendingBindings();
-        holder.itemView.setOnLongClickListener(this);
         if (asyncLoad)
             AsyncImageLoader.LoadImage(new VideoCoverFetcher(holder.binding, media), null);
     }
@@ -334,7 +333,7 @@ public class VideoListAdapter extends RecyclerView.Adapter<VideoListAdapter.View
         return super.getItemViewType(position);
     }
 
-    public class ViewHolder extends RecyclerView.ViewHolder {
+    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnLongClickListener {
         boolean listmode;
         ViewDataBinding binding;
 
@@ -343,6 +342,7 @@ public class VideoListAdapter extends RecyclerView.Adapter<VideoListAdapter.View
             binding = DataBindingUtil.bind(itemView);
             this.listmode = listMode;
             binding.setVariable(BR.holder, this);
+            itemView.setOnLongClickListener(this);
         }
 
         public void onClick(View v){
@@ -361,14 +361,13 @@ public class VideoListAdapter extends RecyclerView.Adapter<VideoListAdapter.View
                 return;
             mFragment.mGridView.openContextMenu(getAdapterPosition());
         }
-    }
 
-    @Override
-    public boolean onLongClick(View v) {
-        if (mFragment == null)
-            return false;
-        int position = ((Integer) v.findViewById(R.id.item_more).getTag()).intValue();
-        mFragment.mGridView.openContextMenu(position);
-        return true;
+        @Override
+        public boolean onLongClick(View v) {
+            if (mFragment == null)
+                return false;
+            mFragment.mGridView.openContextMenu(getLayoutPosition());
+            return true;
+        }
     }
 }



More information about the Android mailing list