[Android] Data binding for album view

Geoffrey Métais git at videolan.org
Mon Sep 7 14:29:23 CEST 2015


vlc-ports/android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Thu Sep  3 17:45:12 2015 +0200| [a26c9b9ebb6a07ed57fdba10facfd6225e5333fd] | committer: Geoffrey Métais

Data binding for album view

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

 vlc-android/res/layout/audio_browser_item.xml      |   10 ++--
 .../org/videolan/vlc/gui/audio/AlbumAdapter.java   |   53 +++++++++-----------
 2 files changed, 31 insertions(+), 32 deletions(-)

diff --git a/vlc-android/res/layout/audio_browser_item.xml b/vlc-android/res/layout/audio_browser_item.xml
index 79d3f65..1ce816a 100644
--- a/vlc-android/res/layout/audio_browser_item.xml
+++ b/vlc-android/res/layout/audio_browser_item.xml
@@ -2,10 +2,14 @@
 <layout xmlns:android="http://schemas.android.com/apk/res/android">
     <data>
         <import type="android.view.View"/>
+        <import type="android.text.TextUtils"/>
         <variable
             name="item"
             type="org.videolan.vlc.gui.audio.AudioBrowserListAdapter.ListItem"/>
         <variable
+            name="media"
+            type="org.videolan.vlc.MediaWrapper"/>
+        <variable
             name="cover"
             type="android.graphics.drawable.BitmapDrawable"/>
         <variable
@@ -54,7 +58,7 @@
                     android:singleLine="true"
                     android:ellipsize="middle"
                     android:maxLines="1"
-                    android:text="@{item.mTitle}"
+                    android:text="@{media.title ?? item.mTitle}"
                     android:fontFamily="sans-serif-light"
                     android:textColor="?attr/list_title"
                     android:textSize="16sp" />
@@ -66,8 +70,8 @@
                     android:singleLine="true"
                     android:ellipsize="middle"
                     android:maxLines="1"
-                    android:text="@{item.mSubTitle}"
-                    android:visibility="@{item.mSubTitle != null ? View.VISIBLE : View.GONE}"
+                    android:text="@{media.artist ?? item.mSubTitle}"
+                    android:visibility="@{TextUtils.isEmpty(item.mSubTitle) && TextUtils.isEmpty(media.artist)  ? View.GONE : View.VISIBLE}"
                     android:fontFamily="sans-serif-light"
                     android:textColor="?attr/list_subtitle" />
             </LinearLayout>
diff --git a/vlc-android/src/org/videolan/vlc/gui/audio/AlbumAdapter.java b/vlc-android/src/org/videolan/vlc/gui/audio/AlbumAdapter.java
index 0c3d871..939a9a1 100644
--- a/vlc-android/src/org/videolan/vlc/gui/audio/AlbumAdapter.java
+++ b/vlc-android/src/org/videolan/vlc/gui/audio/AlbumAdapter.java
@@ -25,20 +25,22 @@ package org.videolan.vlc.gui.audio;
 
 import android.content.Context;
 import android.database.DataSetObserver;
+import android.databinding.DataBindingUtil;
 import android.support.annotation.Nullable;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.ArrayAdapter;
-import android.widget.ImageView;
-import android.widget.TextView;
+import android.widget.LinearLayout;
 
 import org.videolan.vlc.MediaWrapper;
 import org.videolan.vlc.R;
+import org.videolan.vlc.databinding.AudioBrowserItemBinding;
+import org.videolan.vlc.interfaces.IAudioClickHandler;
 
 import java.util.ArrayList;
 
-public class AlbumAdapter extends ArrayAdapter<MediaWrapper> {
+public class AlbumAdapter extends ArrayAdapter<MediaWrapper> implements IAudioClickHandler{
 
     private ArrayList<MediaWrapper> mMediaList;
 
@@ -56,31 +58,20 @@ public class AlbumAdapter extends ArrayAdapter<MediaWrapper> {
         MediaWrapper mw = mMediaList.get(position);
         if (v == null) {
             LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
-            v = inflater.inflate(R.layout.audio_browser_item, parent, false);
             holder = new ViewHolder();
-            holder.title = (TextView) v.findViewById(R.id.title);
-            holder.subtitle = (TextView) v.findViewById(R.id.subtitle);
-            holder.more = (ImageView) v.findViewById(R.id.item_more);
-            holder.footer = v.findViewById(R.id.footer);
+            holder.binding = DataBindingUtil.inflate(inflater, R.layout.audio_browser_item, parent, false);
+            v = holder.binding.getRoot();
 
-            v.setTag(holder);
+            v.setTag(R.layout.audio_browser_item, holder);
         } else
-            holder = (ViewHolder) v.getTag();
-
-        holder.title.setText(mw.getTitle());
-        holder.subtitle.setText(mw.getArtist());
-        holder.footer.setVisibility(position == mMediaList.size() - 1 ? View.INVISIBLE : View.VISIBLE);
-        if (mContextPopupMenuListener != null)
-            holder.more.setOnClickListener(new View.OnClickListener() {
-                @Override
-                public void onClick(View v) {
-                    if (mContextPopupMenuListener != null)
-                        mContextPopupMenuListener.onPopupMenu(v, position);
-                }
-            });
-        else
-            holder.more.setVisibility(View.GONE);
-        v.findViewById(R.id.cover).setVisibility(View.GONE);
+            holder = (ViewHolder) v.getTag(R.layout.audio_browser_item);
+
+        holder.position = position;
+        holder.binding.setMedia(mw);
+        holder.binding.setFooter(position != mMediaList.size() - 1);
+        holder.binding.setClickable(mContextPopupMenuListener != null);
+        holder.binding.setHandler(this);
+        holder.binding.executePendingBindings();
         return v;
     }
 
@@ -108,11 +99,15 @@ public class AlbumAdapter extends ArrayAdapter<MediaWrapper> {
             super.unregisterDataSetObserver(observer);
     }
 
+    @Override
+    public void onMoreClick(View v) {
+        if (mContextPopupMenuListener != null)
+                mContextPopupMenuListener.onPopupMenu(v, ((ViewHolder) ((LinearLayout)v.getParent().getParent()).getTag(R.layout.audio_browser_item)).position);
+    }
+
     static class ViewHolder {
-        TextView title;
-        TextView subtitle;
-        ImageView more;
-        View footer;
+        AudioBrowserItemBinding binding;
+        int position;
     }
 
     public interface ContextPopupMenuListener {



More information about the Android mailing list