[Android] Prefer getLayoutPosition to getAdapterPosition

Geoffrey Métais git at videolan.org
Mon Mar 13 16:45:16 CET 2017


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Mon Mar 13 16:43:43 2017 +0100| [3947152df50522bace84c5834cb1451b0febae8f] | committer: Geoffrey Métais

Prefer getLayoutPosition to getAdapterPosition

getAdapterPosition can return -1 while LayoutManager is updating views

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

 vlc-android/src/org/videolan/vlc/gui/HistoryAdapter.java   |  4 ++--
 .../org/videolan/vlc/gui/audio/AudioBrowserAdapter.java    |  6 +++---
 .../org/videolan/vlc/gui/browser/BaseBrowserAdapter.java   | 14 +++++++-------
 .../videolan/vlc/gui/browser/StorageBrowserAdapter.java    |  4 ++--
 .../src/org/videolan/vlc/gui/dialogs/AdvOptionsDialog.java |  2 +-
 .../vlc/gui/helpers/SwipeDragItemTouchHelperCallback.java  |  4 ++--
 .../src/org/videolan/vlc/gui/video/VideoListAdapter.java   |  8 ++++----
 7 files changed, 21 insertions(+), 21 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/gui/HistoryAdapter.java b/vlc-android/src/org/videolan/vlc/gui/HistoryAdapter.java
index 8196246..3bc524a 100644
--- a/vlc-android/src/org/videolan/vlc/gui/HistoryAdapter.java
+++ b/vlc-android/src/org/videolan/vlc/gui/HistoryAdapter.java
@@ -56,12 +56,12 @@ public class HistoryAdapter extends RecyclerView.Adapter<HistoryAdapter.ViewHold
         }
 
         public void onClick(View v){
-            int position = getAdapterPosition();
+            int position = getLayoutPosition();
             mEventsHandler.onClick(v, position, mMediaList.get(position));
         }
 
         public boolean onLongClick(View v) {
-            int position = getAdapterPosition();
+            int position = getLayoutPosition();
             return mEventsHandler.onLongClick(v, position, mMediaList.get(position));
         }
     }
diff --git a/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserAdapter.java b/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserAdapter.java
index 691d567..bd8bcc4 100644
--- a/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserAdapter.java
+++ b/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserAdapter.java
@@ -363,20 +363,20 @@ public class AudioBrowserAdapter extends BaseQueuedAdapter<MediaLibraryItem[], A
 
         public void onClick(View v) {
             if (mIEventsHandler != null) {
-                int position = getAdapterPosition();
+                int position = getLayoutPosition();
                 mIEventsHandler.onClick(v, position, mDataList[position]);
             }
         }
 
         public void onMoreClick(View v) {
             if (mIEventsHandler != null) {
-                int position = getAdapterPosition();
+                int position = getLayoutPosition();
                 mIEventsHandler.onCtxClick(v, position, mDataList[position]);
             }
         }
 
         public boolean onLongClick(View view) {
-            int position = getAdapterPosition();
+            int position = getLayoutPosition();
             return mIEventsHandler.onLongClick(view, position, mDataList[position]);
         }
 
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 cd4091f..0193971 100644
--- a/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserAdapter.java
+++ b/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserAdapter.java
@@ -161,14 +161,14 @@ public class BaseBrowserAdapter extends BaseQueuedAdapter<ArrayList<MediaLibrary
         }
 
         protected void openStorage() {
-            MediaWrapper mw = new MediaWrapper(((Storage) getItem(getAdapterPosition())).getUri());
+            MediaWrapper mw = new MediaWrapper(((Storage) getItem(getLayoutPosition())).getUri());
             mw.setType(MediaWrapper.TYPE_DIR);
-            fragment.browse(mw, getAdapterPosition(), binding.browserCheckbox.isChecked());
+            fragment.browse(mw, getLayoutPosition(), binding.browserCheckbox.isChecked());
         }
 
         public void onCheckBoxClick(View v) {
-            if (getItem(getAdapterPosition()).getItemType() == TYPE_STORAGE)
-                checkBoxAction(v, ((Storage) getItem(getAdapterPosition())).getUri().getPath());
+            if (getItem(getLayoutPosition()).getItemType() == TYPE_STORAGE)
+                checkBoxAction(v, ((Storage) getItem(getLayoutPosition())).getUri().getPath());
         }
 
         @Override
@@ -177,18 +177,18 @@ public class BaseBrowserAdapter extends BaseQueuedAdapter<ArrayList<MediaLibrary
         }
 
         public void onClick(View v){
-            int position = getAdapterPosition();
+            int position = getLayoutPosition();
             fragment.onClick(v, position, mMediaList.get(position));
         }
 
         public void onMoreClick(View v) {
-            int position = getAdapterPosition();
+            int position = getLayoutPosition();
             fragment.onCtxClick(v, position, mMediaList.get(position));
         }
 
         @Override
         public boolean onLongClick(View v) {
-            int position = getAdapterPosition();
+            int position = getLayoutPosition();
             return fragment.onLongClick(v, position, mMediaList.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 58a4e38..f377ff1 100644
--- a/vlc-android/src/org/videolan/vlc/gui/browser/StorageBrowserAdapter.java
+++ b/vlc-android/src/org/videolan/vlc/gui/browser/StorageBrowserAdapter.java
@@ -101,9 +101,9 @@ class StorageBrowserAdapter extends BaseBrowserAdapter {
     }
 
     protected void openMediaFromView(MediaViewHolder holder, View v) {
-        MediaWrapper mw = new MediaWrapper(((Storage) getItem(holder.getAdapterPosition())).getUri());
+        MediaWrapper mw = new MediaWrapper(((Storage) getItem(holder.getLayoutPosition())).getUri());
         mw.setType(MediaWrapper.TYPE_DIR);
-        fragment.browse(mw, holder.getAdapterPosition(), holder.binding.browserCheckbox.isChecked());
+        fragment.browse(mw, holder.getLayoutPosition(), holder.binding.browserCheckbox.isChecked());
     }
 
     protected void checkBoxAction(View v, String path){
diff --git a/vlc-android/src/org/videolan/vlc/gui/dialogs/AdvOptionsDialog.java b/vlc-android/src/org/videolan/vlc/gui/dialogs/AdvOptionsDialog.java
index d5b528b..3c4bf64 100644
--- a/vlc-android/src/org/videolan/vlc/gui/dialogs/AdvOptionsDialog.java
+++ b/vlc-android/src/org/videolan/vlc/gui/dialogs/AdvOptionsDialog.java
@@ -705,7 +705,7 @@ public class AdvOptionsDialog extends DialogFragment implements View.OnClickList
 
             @Override
             public boolean onLongClick(View view) {
-                Toast toast = Toast.makeText(VLCApplication.getAppContext(), mList.get(getAdapterPosition()).text, Toast.LENGTH_SHORT);
+                Toast toast = Toast.makeText(VLCApplication.getAppContext(), mList.get(getLayoutPosition()).text, Toast.LENGTH_SHORT);
                 toast.setGravity(Gravity.TOP,0,0);
                 toast.show();
                 return true;
diff --git a/vlc-android/src/org/videolan/vlc/gui/helpers/SwipeDragItemTouchHelperCallback.java b/vlc-android/src/org/videolan/vlc/gui/helpers/SwipeDragItemTouchHelperCallback.java
index fb295e2..2dd2364 100644
--- a/vlc-android/src/org/videolan/vlc/gui/helpers/SwipeDragItemTouchHelperCallback.java
+++ b/vlc-android/src/org/videolan/vlc/gui/helpers/SwipeDragItemTouchHelperCallback.java
@@ -47,12 +47,12 @@ public class SwipeDragItemTouchHelperCallback extends ItemTouchHelper.Callback {
 
     @Override
     public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
-        mAdapter.onItemMove(viewHolder.getAdapterPosition(), target.getAdapterPosition());
+        mAdapter.onItemMove(viewHolder.getLayoutPosition(), target.getLayoutPosition());
         return true;
     }
 
     @Override
     public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
-        mAdapter.onItemDismiss(viewHolder.getAdapterPosition());
+        mAdapter.onItemDismiss(viewHolder.getLayoutPosition());
     }
 }
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 7875687..d05409f 100644
--- a/vlc-android/src/org/videolan/vlc/gui/video/VideoListAdapter.java
+++ b/vlc-android/src/org/videolan/vlc/gui/video/VideoListAdapter.java
@@ -329,16 +329,16 @@ public class VideoListAdapter extends RecyclerView.Adapter<VideoListAdapter.View
         }
 
         public void onClick(View v) {
-            int position = getAdapterPosition();
+            int position = getLayoutPosition();
             mEventsHandler.onClick(v, position, mVideos.get(position));
         }
 
         public void onMoreClick(View v){
-            mEventsHandler.onCtxClick(v, getAdapterPosition(), null);
+            mEventsHandler.onCtxClick(v, getLayoutPosition(), null);
         }
 
         public boolean onLongClick(View v) {
-            int position = getAdapterPosition();
+            int position = getLayoutPosition();
             return mEventsHandler.onLongClick(v, position, mVideos.get(position));
         }
 
@@ -348,7 +348,7 @@ public class VideoListAdapter extends RecyclerView.Adapter<VideoListAdapter.View
 
         @Override
         public void onFocusChange(View v, boolean hasFocus) {
-            setViewBackground(hasFocus || mVideos.get(getAdapterPosition()).hasStateFlags(MediaLibraryItem.FLAG_SELECTED));
+            setViewBackground(hasFocus || mVideos.get(getLayoutPosition()).hasStateFlags(MediaLibraryItem.FLAG_SELECTED));
         }
 
         private void setViewBackground(boolean highlight) {



More information about the Android mailing list