[Android] Refactor History item layout with ConstraintLayout
Geoffrey Métais
git at videolan.org
Sun Feb 5 13:07:55 CET 2017
vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Sun Feb 5 13:06:31 2017 +0100| [871f006b9ed93f10f014547db5a6713e6774c1fb] | committer: Geoffrey Métais
Refactor History item layout with ConstraintLayout
> https://code.videolan.org/videolan/vlc-android/commit/871f006b9ed93f10f014547db5a6713e6774c1fb
---
vlc-android/res/layout/history_item.xml | 65 +++++++++++++++++++
vlc-android/res/layout/list_item.xml | 74 ----------------------
.../src/org/videolan/vlc/gui/HistoryAdapter.java | 20 +++---
3 files changed, 75 insertions(+), 84 deletions(-)
diff --git a/vlc-android/res/layout/history_item.xml b/vlc-android/res/layout/history_item.xml
new file mode 100644
index 0000000..ffef665
--- /dev/null
+++ b/vlc-android/res/layout/history_item.xml
@@ -0,0 +1,65 @@
+<?xml version="1.0" encoding="utf-8"?>
+<layout xmlns:app="http://schemas.android.com/apk/res-auto"
+ xmlns:tools="http://schemas.android.com/tools"
+ xmlns:android="http://schemas.android.com/apk/res/android" >
+ <data>
+ <import type="org.videolan.medialibrary.media.MediaWrapper"/>
+ <import type="android.text.TextUtils"/>
+ <import type="android.view.View"/>
+ <variable
+ name="holder"
+ type="org.videolan.vlc.gui.HistoryAdapter.ViewHolder"/>
+ <variable
+ name="media"
+ type="MediaWrapper"/>
+ <variable
+ name="bgColor"
+ type="int" />
+ </data>
+<android.support.constraint.ConstraintLayout
+ android:layout_width="match_parent"
+ android:layout_height="wrap_content"
+ android:background="@{bgColor}"
+ android:clickable="true"
+ android:onClick="@{holder::onClick}"
+ android:onLongClick="@{holder::onLongClick}" >
+ <ImageView
+ android:id="@+id/icon"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:scaleType="centerCrop"
+ android:src="@{media.type == MediaWrapper.TYPE_VIDEO ? @drawable/ic_browser_video_normal : @drawable/ic_browser_audio_normal}"
+ app:layout_constraintLeft_toLeftOf="parent" />
+
+ <TextView
+ android:id="@+id/title"
+ android:layout_width="0dp"
+ android:layout_height="wrap_content"
+ android:text="@{media.title}"
+ android:fontFamily="sans-serif-light"
+ android:textColor="?attr/list_title"
+ android:textSize="16sp"
+ android:maxLines="1"
+ app:layout_constraintBottom_toTopOf="@+id/subtitle"
+ app:layout_constraintTop_toTopOf="parent"
+ app:layout_constraintStart_toEndOf="@id/icon"
+ app:layout_constraintEnd_toEndOf="parent"
+ app:layout_constraintVertical_chainStyle="packed" />
+
+ <TextView
+ android:id="@+id/subtitle"
+ android:layout_width="0dp"
+ android:layout_height="wrap_content"
+ android:text="@{media.artist}"
+ android:fontFamily="sans-serif-light"
+ android:textColor="?attr/list_subtitle"
+ android:maxLines="1"
+ android:ellipsize="start"
+ android:visibility="@{TextUtils.isEmpty(media.artist) ? View.GONE : View.VISIBLE}"
+ app:layout_constraintBottom_toBottomOf="parent"
+ app:layout_constraintTop_toBottomOf="@id/title"
+ app:layout_constraintStart_toEndOf="@id/icon"
+ app:layout_constraintEnd_toEndOf="parent"/>
+
+</android.support.constraint.ConstraintLayout>
+</layout>
\ No newline at end of file
diff --git a/vlc-android/res/layout/list_item.xml b/vlc-android/res/layout/list_item.xml
deleted file mode 100644
index 6fa3b93..0000000
--- a/vlc-android/res/layout/list_item.xml
+++ /dev/null
@@ -1,74 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<layout xmlns:android="http://schemas.android.com/apk/res/android" >
- <data>
- <import type="org.videolan.medialibrary.media.MediaWrapper"/>
- <import type="android.text.TextUtils"/>
- <import type="android.view.View"/>
- <variable
- name="holder"
- type="org.videolan.vlc.gui.HistoryAdapter.ViewHolder"/>
- <variable
- name="media"
- type="MediaWrapper"/>
- <variable
- name="position"
- type="int"/>
-
- <variable
- name="bgColor"
- type="int" />
- </data>
- <LinearLayout
- android:id="@+id/layout_item"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_gravity="center_vertical"
- android:padding="5dp"
- android:layout_marginLeft="5dp"
- android:layout_marginRight="5dp"
- android:orientation="horizontal"
- android:background="@{bgColor}"
- android:clickable="true"
- android:onClick="@{holder::onClick}"
- android:onLongClick="@{holder::onLongClick}" >
-
- <ImageView
- android:id="@+id/icon"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center_vertical"
- android:scaleType="centerCrop"
- android:src="@{media.type == MediaWrapper.TYPE_VIDEO ? @drawable/ic_browser_video_normal : @drawable/ic_browser_audio_normal}" />
-
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_gravity="center_vertical"
- android:padding="5dp"
- android:orientation="vertical" >
-
- <TextView
- android:id="@+id/title"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@{media.title}"
- android:layout_gravity="start"
- android:fontFamily="sans-serif-light"
- android:textColor="?attr/list_title"
- android:textSize="16sp"
- android:maxLines="1" />
-
- <TextView
- android:id="@+id/subtitle"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@{media.artist}"
- android:layout_gravity="start"
- android:fontFamily="sans-serif-light"
- android:textColor="?attr/list_subtitle"
- android:maxLines="1"
- android:ellipsize="start"
- android:visibility="@{TextUtils.isEmpty(media.artist) ? View.GONE : View.VISIBLE}"/>
- </LinearLayout>
- </LinearLayout>
-</layout>
\ No newline at end of file
diff --git a/vlc-android/src/org/videolan/vlc/gui/HistoryAdapter.java b/vlc-android/src/org/videolan/vlc/gui/HistoryAdapter.java
index ecab6dc..8196246 100644
--- a/vlc-android/src/org/videolan/vlc/gui/HistoryAdapter.java
+++ b/vlc-android/src/org/videolan/vlc/gui/HistoryAdapter.java
@@ -19,7 +19,6 @@
*****************************************************************************/
package org.videolan.vlc.gui;
-import android.databinding.DataBindingUtil;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
@@ -29,7 +28,7 @@ import android.view.ViewGroup;
import org.videolan.medialibrary.media.MediaLibraryItem;
import org.videolan.medialibrary.media.MediaWrapper;
import org.videolan.vlc.R;
-import org.videolan.vlc.databinding.ListItemBinding;
+import org.videolan.vlc.databinding.HistoryItemBinding;
import org.videolan.vlc.gui.helpers.UiTools;
import org.videolan.vlc.interfaces.IEventsHandler;
import org.videolan.vlc.util.Util;
@@ -45,13 +44,15 @@ public class HistoryAdapter extends RecyclerView.Adapter<HistoryAdapter.ViewHold
private IEventsHandler mEventsHandler;
private ArrayList<MediaWrapper> mMediaList = new ArrayList<>();
+ private LayoutInflater mLayoutInflater;
public class ViewHolder extends RecyclerView.ViewHolder {
- ListItemBinding binding;
+ HistoryItemBinding binding;
- public ViewHolder(View itemView) {
- super(itemView);
- binding = DataBindingUtil.bind(itemView);
+ public ViewHolder(HistoryItemBinding binding) {
+ super(binding.getRoot());
+ this.binding = binding;
+ binding.setHolder(this);
}
public void onClick(View v){
@@ -89,9 +90,9 @@ public class HistoryAdapter extends RecyclerView.Adapter<HistoryAdapter.ViewHold
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
- View v = LayoutInflater.from(parent.getContext())
- .inflate(R.layout.list_item, parent, false);
- return new ViewHolder(v);
+ if (mLayoutInflater == null)
+ mLayoutInflater = LayoutInflater.from(parent.getContext());
+ return new ViewHolder(HistoryItemBinding.inflate(mLayoutInflater, parent, false));
}
@Override
@@ -99,7 +100,6 @@ public class HistoryAdapter extends RecyclerView.Adapter<HistoryAdapter.ViewHold
final MediaWrapper media = mMediaList.get(position);
boolean isSelected = media.hasStateFlags(MediaLibraryItem.FLAG_SELECTED);
holder.binding.setMedia(media);
- holder.binding.setHolder(holder);
holder.binding.setBgColor(ContextCompat.getColor(holder.itemView.getContext(), isSelected ? R.color.orange200transparent : R.color.transparent));
}
More information about the Android
mailing list