[Android] Refactor info tracks list into RecyclerView

Geoffrey Métais git at videolan.org
Fri Jul 7 17:16:34 CEST 2017


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Fri Jul  7 16:17:18 2017 +0200| [04868ab6640c7d1045fd802604a46acc1310f686] | committer: Geoffrey Métais

Refactor info tracks list into RecyclerView

Close #295

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

 vlc-android/res/layout/info_activity.xml           |  4 +-
 .../src/org/videolan/vlc/gui/InfoActivity.java     | 18 +++---
 .../videolan/vlc/gui/video/MediaInfoAdapter.java   | 66 ++++++++++++----------
 3 files changed, 48 insertions(+), 40 deletions(-)

diff --git a/vlc-android/res/layout/info_activity.xml b/vlc-android/res/layout/info_activity.xml
index b32c8f4d5..58500388e 100644
--- a/vlc-android/res/layout/info_activity.xml
+++ b/vlc-android/res/layout/info_activity.xml
@@ -187,7 +187,7 @@
                 vlc:layout_constraintEnd_toEndOf="parent"
                 android:src="@drawable/ic_audiosub_circle_normal_o"
                 android:visibility="gone" />
-            <ListView
+            <android.support.v7.widget.RecyclerView
                 android:id="@+id/list"
                 android:layout_width="0dp"
                 android:layout_height="0dp"
@@ -195,9 +195,7 @@
                 vlc:layout_constraintBottom_toBottomOf="parent"
                 vlc:layout_constraintStart_toStartOf="parent"
                 vlc:layout_constraintEnd_toEndOf="parent"
-                android:cacheColorHint="#00000000"
                 android:clipToPadding="false"
-                android:divider="#00000000"
                 android:paddingBottom="@dimen/listview_bottom_padding"
                 android:visibility="@{item.itemType == MediaLibraryItem.TYPE_MEDIA ? View.VISIBLE : View.INVISIBLE}" />
 
diff --git a/vlc-android/src/org/videolan/vlc/gui/InfoActivity.java b/vlc-android/src/org/videolan/vlc/gui/InfoActivity.java
index d3810190a..c944364c9 100644
--- a/vlc-android/src/org/videolan/vlc/gui/InfoActivity.java
+++ b/vlc-android/src/org/videolan/vlc/gui/InfoActivity.java
@@ -13,6 +13,7 @@ import android.support.annotation.Nullable;
 import android.support.design.widget.BottomSheetBehavior;
 import android.support.design.widget.CoordinatorLayout;
 import android.support.v4.view.ViewCompat;
+import android.support.v7.widget.LinearLayoutManager;
 import android.text.TextUtils;
 import android.view.Gravity;
 import android.view.View;
@@ -38,6 +39,8 @@ import org.videolan.vlc.util.VLCInstance;
 import org.videolan.vlc.util.WeakHandler;
 
 import java.io.File;
+import java.util.LinkedList;
+import java.util.List;
 
 public class InfoActivity extends AudioPlayerContainerActivity implements View.OnClickListener {
 
@@ -98,7 +101,8 @@ public class InfoActivity extends AudioPlayerContainerActivity implements View.O
             noCoverFallback();
         mBinding.fab.setOnClickListener(this);
         if (mItem.getItemType() == MediaLibraryItem.TYPE_MEDIA) {
-            mAdapter = new MediaInfoAdapter(this);
+            mAdapter = new MediaInfoAdapter();
+            mBinding.list.setLayoutManager(new LinearLayoutManager(mBinding.getRoot().getContext()));
             mBinding.list.setAdapter(mAdapter);
             mCheckFileTask = (CheckFileTask) new CheckFileTask().execute();
             mParseTracksTask = (ParseTracksTask) new ParseTracksTask().execute();
@@ -273,20 +277,18 @@ public class InfoActivity extends AudioPlayerContainerActivity implements View.O
         @Override
         protected void onPostExecute(Media media) {
             mParseTracksTask = null;
-            if (isCancelled())
+            if (media == null || isCancelled())
                 return;
             boolean hasSubs = false;
-            if (media == null)
-                return;
             final int trackCount = media.getTrackCount();
+            List<Media.Track> tracks = new LinkedList<>();
             for (int i = 0; i < trackCount; ++i) {
                 final Media.Track track = media.getTrack(i);
-                if (track.type == Media.Track.Type.Text)
-                    hasSubs = true;
-                mAdapter.add(track);
+                tracks.add(track);
+                hasSubs |= track.type == Media.Track.Type.Text;
             }
             media.release();
-
+            mAdapter.setTracks(tracks);
             if (hasSubs)
                 mBinding.infoSubtitles.setVisibility(View.VISIBLE);
         }
diff --git a/vlc-android/src/org/videolan/vlc/gui/video/MediaInfoAdapter.java b/vlc-android/src/org/videolan/vlc/gui/video/MediaInfoAdapter.java
index 87186e0ba..96405352a 100644
--- a/vlc-android/src/org/videolan/vlc/gui/video/MediaInfoAdapter.java
+++ b/vlc-android/src/org/videolan/vlc/gui/video/MediaInfoAdapter.java
@@ -1,7 +1,7 @@
 /*****************************************************************************
  * MediaInfoAdapter.java
  *****************************************************************************
- * Copyright © 2011-2015 VLC authors and VideoLAN
+ * Copyright © 2011-2017 VLC authors and VideoLAN
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -22,42 +22,36 @@ package org.videolan.vlc.gui.video;
 
 import android.content.Context;
 import android.content.res.Resources;
+import android.support.v7.widget.RecyclerView;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
-import android.widget.ArrayAdapter;
 import android.widget.TextView;
 
 import org.videolan.libvlc.Media;
 import org.videolan.vlc.R;
 import org.videolan.vlc.util.Strings;
 
-public class MediaInfoAdapter extends ArrayAdapter<Media.Track> {
+import java.util.List;
 
-    public MediaInfoAdapter(Context context) {
-        super(context, 0);
-    }
+public class MediaInfoAdapter extends RecyclerView.Adapter<MediaInfoAdapter.ViewHolder> {
+    private LayoutInflater inflater;
+    private List<Media.Track> mDataset;
 
     @Override
-    public View getView(int position, View convertView, ViewGroup parent) {
-        ViewHolder holder;
-        View v = convertView;
-        if (v == null) {
-            LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
-            v = inflater.inflate(R.layout.info_item, parent, false);
-            holder = new ViewHolder();
-            holder.title = (TextView) v.findViewById(R.id.title);
-            holder.text = (TextView) v.findViewById(R.id.subtitle);
-            v.setTag(holder);
-        } else
-            holder = (ViewHolder) v.getTag();
+    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
+        if (inflater == null)
+            inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
+        return new ViewHolder(inflater.inflate(R.layout.info_item, parent, false));
+    }
 
-        Media.Track track = getItem(position);
+    @Override
+    public void onBindViewHolder(ViewHolder holder, int position) {
+        Media.Track track = mDataset.get(position);
         String title;
-        StringBuilder textBuilder = new StringBuilder(1024);
-        Resources res = getContext().getResources();
-        switch (track.type)
-        {
+        StringBuilder textBuilder = new StringBuilder();
+        Resources res = holder.itemView.getContext().getResources();
+        switch (track.type) {
             case Media.Track.Type.Audio:
                 title = res.getString(R.string.track_audio);
                 appendCommon(textBuilder, res, track);
@@ -75,11 +69,21 @@ public class MediaInfoAdapter extends ArrayAdapter<Media.Track> {
             default:
                 title = res.getString(R.string.track_unknown);
         }
-
         holder.title.setText(title);
         holder.text.setText(textBuilder.toString());
+    }
 
-        return v;
+    @Override
+    public int getItemCount() {
+        return mDataset == null ? 0 : mDataset.size();
+    }
+
+    public void setTracks(List<Media.Track> tracks) {
+        int size = getItemCount();
+        mDataset = tracks;
+        if (size > 0)
+            notifyItemRangeRemoved(0, size-1);
+        notifyItemRangeInserted(0, tracks.size()-1);
     }
 
     private void appendCommon(StringBuilder textBuilder, Resources res, Media.Track track) {
@@ -103,9 +107,13 @@ public class MediaInfoAdapter extends ArrayAdapter<Media.Track> {
             textBuilder.append(res.getString(R.string.track_framerate_info, framerate));
     }
 
-    static class ViewHolder {
-        TextView title;
-        TextView text;
-    }
+    class ViewHolder extends RecyclerView.ViewHolder {
+        private TextView title, text;
 
+        public ViewHolder(View itemView) {
+            super(itemView);
+            title = itemView.findViewById(R.id.title);
+            text = itemView.findViewById(R.id.subtitle);
+        }
+    }
 }



More information about the Android mailing list