[Android] Refactor longpress in browsers

Geoffrey Métais git at videolan.org
Thu Feb 8 15:44:08 CET 2018


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Thu Feb  8 12:44:14 2018 +0100| [dc8a38748e6ac20db7814f57b01e1ed53e5c4604] | committer: Geoffrey Métais

Refactor longpress in browsers

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

 vlc-android/res/layout/browser_item.xml            |  1 +
 .../vlc/gui/browser/BaseBrowserAdapter.java        | 23 ++++++++--------------
 .../vlc/gui/browser/StorageBrowserAdapter.java     |  2 --
 3 files changed, 9 insertions(+), 17 deletions(-)

diff --git a/vlc-android/res/layout/browser_item.xml b/vlc-android/res/layout/browser_item.xml
index f8617399a..6153548d9 100644
--- a/vlc-android/res/layout/browser_item.xml
+++ b/vlc-android/res/layout/browser_item.xml
@@ -38,6 +38,7 @@
         android:clickable="@{holder != null}"
         android:longClickable="@{hasContextMenu || item.getItemType() == MediaLibraryItem.TYPE_STORAGE}"
         android:onClick="@{holder::onClick}"
+        android:onLongClick="@{holder::onLongClick}"
         android:background="@{bgColor}" >
 
         <org.videolan.vlc.gui.helpers.ThreeStatesCheckbox
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 39a2b97cf..e83169b83 100644
--- a/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserAdapter.java
+++ b/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserAdapter.java
@@ -127,7 +127,6 @@ public class BaseBrowserAdapter extends SortableAdapter<MediaLibraryItem, BaseBr
         if (mNetworkRoot)
             vh.binding.setProtocol(getProtocol(media));
         vh.binding.setCover(getIcon(media, mSpecialIcons));
-        vh.setContextMenuListener();
         vh.selectView(media.hasStateFlags(FLAG_SELECTED));
     }
 
@@ -148,6 +147,8 @@ public class BaseBrowserAdapter extends SortableAdapter<MediaLibraryItem, BaseBr
 
         public void onClick(View v){}
 
+        public boolean onLongClick(View v){ return false; }
+
         public void onCheckBoxClick(View v){}
 
         public void onMoreClick(View v){}
@@ -156,19 +157,11 @@ public class BaseBrowserAdapter extends SortableAdapter<MediaLibraryItem, BaseBr
 
     }
 
-    class MediaViewHolder extends ViewHolder<BrowserItemBinding> implements View.OnLongClickListener, View.OnFocusChangeListener {
+    class MediaViewHolder extends ViewHolder<BrowserItemBinding> implements View.OnFocusChangeListener {
 
         MediaViewHolder(final BrowserItemBinding binding) {
             super(binding);
             binding.setHolder(this);
-            itemView.setOnLongClickListener(new View.OnLongClickListener() {
-                @Override
-                public boolean onLongClick(View v) {
-                    binding.browserCheckbox.toggle();
-                    onCheckBoxClick(binding.browserCheckbox);
-                    return true;
-                }
-            });
             if (AndroidUtil.isMarshMallowOrLater) itemView.setOnContextClickListener(new View.OnContextClickListener() {
                 @Override
                 public boolean onContextClick(View v) {
@@ -178,10 +171,6 @@ public class BaseBrowserAdapter extends SortableAdapter<MediaLibraryItem, BaseBr
             });
         }
 
-        void setContextMenuListener() {
-            itemView.setOnLongClickListener(this);
-        }
-
         protected void openStorage() {
             MediaWrapper mw = new MediaWrapper(((Storage) getItem(getLayoutPosition())).getUri());
             mw.setType(MediaWrapper.TYPE_DIR);
@@ -210,9 +199,13 @@ public class BaseBrowserAdapter extends SortableAdapter<MediaLibraryItem, BaseBr
                 fragment.onCtxClick(v, position, getDataset().get(position));
         }
 
-        @Override
         public boolean onLongClick(View v) {
             int position = getLayoutPosition();
+            if (getItem(position).getItemType() == TYPE_STORAGE) {
+                binding.browserCheckbox.toggle();
+                onCheckBoxClick(binding.browserCheckbox);
+                return true;
+            }
             return position < getDataset().size() && position >= 0
                     && fragment.onLongClick(v, position, getDataset().get(position));
         }
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 ee72fba8c..d81b34469 100644
--- a/vlc-android/src/org/videolan/vlc/gui/browser/StorageBrowserAdapter.java
+++ b/vlc-android/src/org/videolan/vlc/gui/browser/StorageBrowserAdapter.java
@@ -70,8 +70,6 @@ class StorageBrowserAdapter extends BaseBrowserAdapter {
         else
             vh.binding.browserCheckbox.setState(ThreeStatesCheckbox.STATE_UNCHECKED);
         vh.binding.setCheckEnabled(!((StorageBrowserFragment) fragment).mScannedDirectory);
-        if (hasContextMenu)
-            vh.setContextMenuListener();
     }
 
     private boolean hasDiscoveredChildren(String path) {



More information about the Android mailing list