[Android] Get up-to-date position in RecyclerView adapters

Geoffrey Métais git at videolan.org
Fri Mar 6 14:24:23 CET 2015


vlc-ports/android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Fri Mar  6 12:00:11 2015 +0100| [48ba779a6779e349fac4dda7a6f9cde005feab83] | committer: Geoffrey Métais

Get up-to-date position in RecyclerView adapters

> http://git.videolan.org/gitweb.cgi/vlc-ports/android.git/?a=commit;h=48ba779a6779e349fac4dda7a6f9cde005feab83
---

 vlc-android/src/org/videolan/vlc/gui/MRLAdapter.java       |   10 +++++-----
 .../src/org/videolan/vlc/gui/network/NetworkAdapter.java   |   12 +++++++-----
 .../src/org/videolan/vlc/gui/network/NetworkFragment.java  |    2 +-
 3 files changed, 13 insertions(+), 11 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/gui/MRLAdapter.java b/vlc-android/src/org/videolan/vlc/gui/MRLAdapter.java
index 80cf0c4..f3165a9 100644
--- a/vlc-android/src/org/videolan/vlc/gui/MRLAdapter.java
+++ b/vlc-android/src/org/videolan/vlc/gui/MRLAdapter.java
@@ -23,7 +23,6 @@ import android.support.v7.widget.RecyclerView;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
-import android.widget.ImageButton;
 import android.widget.ImageView;
 import android.widget.TextView;
 
@@ -60,7 +59,7 @@ public class MRLAdapter extends RecyclerView.Adapter<MRLAdapter.ViewHolder> {
     }
 
     @Override
-    public void onBindViewHolder(ViewHolder holder, final int position) {
+    public void onBindViewHolder(final ViewHolder holder, int position) {
         final String uri = mDataset.get(position);
         holder.uriTv.setText(uri);
         holder.uriTv.setOnClickListener(new View.OnClickListener() {
@@ -72,9 +71,10 @@ public class MRLAdapter extends RecyclerView.Adapter<MRLAdapter.ViewHolder> {
         holder.deleteButton.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
-                MediaDatabase.getInstance().deleteMrlUri(uri);
-                mDataset.remove(position);
-                notifyDataSetChanged(); //because position is not updated
+                int pos = holder.getPosition();
+                MediaDatabase.getInstance().deleteMrlUri(mDataset.get(pos));
+                mDataset.remove(pos);
+                notifyItemRemoved(pos);
             }
         });
     }
diff --git a/vlc-android/src/org/videolan/vlc/gui/network/NetworkAdapter.java b/vlc-android/src/org/videolan/vlc/gui/network/NetworkAdapter.java
index 975dcb4..32cc811 100644
--- a/vlc-android/src/org/videolan/vlc/gui/network/NetworkAdapter.java
+++ b/vlc-android/src/org/videolan/vlc/gui/network/NetworkAdapter.java
@@ -68,10 +68,10 @@ public class NetworkAdapter extends  RecyclerView.Adapter<RecyclerView.ViewHolde
     }
 
     @Override
-    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
+    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
         if (holder instanceof MediaViewHolder) {
             MediaViewHolder vh = (MediaViewHolder) holder;
-            final MediaWrapper media = (MediaWrapper) getItem(position);
+            MediaWrapper media = (MediaWrapper) getItem(position);
             vh.title.setText(media.getTitle());
             vh.text.setVisibility(View.GONE);
             vh.icon.setImageResource(getIconResId(media));
@@ -79,10 +79,11 @@ public class NetworkAdapter extends  RecyclerView.Adapter<RecyclerView.ViewHolde
             vh.itemView.setOnClickListener(new View.OnClickListener() {
                 @Override
                 public void onClick(View v) {
-                    if (media.getType() == MediaWrapper.TYPE_DIR)
-                        fragment.browse(media);
+                    MediaWrapper mw = (MediaWrapper) getItem(holder.getPosition());
+                    if (mw.getType() == MediaWrapper.TYPE_DIR)
+                        fragment.browse(mw);
                     else
-                        Util.openMedia(v.getContext(), media);
+                        Util.openMedia(v.getContext(), mw);
                 }
             });
         } else {
@@ -149,6 +150,7 @@ public class NetworkAdapter extends  RecyclerView.Adapter<RecyclerView.ViewHolde
 
     public void removeItem(int position){
         mMediaList.remove(position);
+        notifyItemRemoved(position);
     }
 
     public Object getItem(int position){
diff --git a/vlc-android/src/org/videolan/vlc/gui/network/NetworkFragment.java b/vlc-android/src/org/videolan/vlc/gui/network/NetworkFragment.java
index 4ee7cc9..7c8d736 100644
--- a/vlc-android/src/org/videolan/vlc/gui/network/NetworkFragment.java
+++ b/vlc-android/src/org/videolan/vlc/gui/network/NetworkFragment.java
@@ -162,7 +162,7 @@ public class NetworkFragment extends BrowserFragment implements IRefreshable, Me
 
     @Override
     public void onMediaRemoved(int index, Media media) {
-        //TODO
+        mAdapter.removeItem(index);
     }
 
     @Override



More information about the Android mailing list