Create a custom layout for the song selector items

Makes the code nicer, speeds things a little, and allows text to be centered
better.

Also add a high DPI expander icon
This commit is contained in:
Christopher Eby 2010-03-10 22:42:44 -06:00
parent e8f8ff3888
commit fe5ff05bad
6 changed files with 173 additions and 81 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB

View File

@ -26,22 +26,14 @@ import java.util.regex.Pattern;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
public abstract class AbstractAdapter extends BaseAdapter implements Filterable {
public static final int ONE_LINE = 0x1;
private Context mContext;
private OnClickListener mExpanderListener;
@ -52,9 +44,6 @@ public abstract class AbstractAdapter extends BaseAdapter implements Filterable
private int mLimiterId = -1;
private CharSequence mLastFilter;
private float mSize;
private int mPadding;
private int mDrawFlags;
private int mMediaField;
@ -64,10 +53,6 @@ public abstract class AbstractAdapter extends BaseAdapter implements Filterable
mAllObjects = allObjects;
mDrawFlags = drawFlags;
mMediaField = mediaField;
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
mSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 14, metrics);
mPadding = (int)mSize / 2;
}
public void setExpanderListener(View.OnClickListener listener)
@ -81,69 +66,24 @@ public abstract class AbstractAdapter extends BaseAdapter implements Filterable
return true;
}
protected abstract void updateText(int position, TextView upper, TextView lower);
protected abstract void updateView(int position, MediaView view);
public View getView(int position, View convertView, ViewGroup parent)
{
RelativeLayout view = null;
MediaView view = null;
try {
view = (RelativeLayout)convertView;
view = (MediaView)convertView;
} catch (ClassCastException e) {
}
if (view == null) {
view = new RelativeLayout(mContext);
view = new MediaView(mContext, mDrawFlags);
RelativeLayout.LayoutParams params;
if (mExpanderListener != null) {
params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
params.addRule(RelativeLayout.CENTER_VERTICAL);
ImageView button = new ImageView(mContext);
button.setPadding(mPadding * 2, mPadding, mPadding, mPadding);
button.setImageResource(R.drawable.expander_arrow);
button.setId(3);
button.setLayoutParams(params);
button.setTag(R.id.field, mMediaField);
button.setOnClickListener(mExpanderListener);
view.addView(button);
}
params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
params.addRule(RelativeLayout.LEFT_OF, 3);
TextView title = new TextView(mContext);
title.setPadding(mPadding, mPadding, 0, (mDrawFlags & ONE_LINE) == 0 ? 0 : mPadding);
title.setSingleLine();
title.setTextColor(Color.WHITE);
title.setTextSize(mSize);
title.setId(1);
title.setLayoutParams(params);
view.addView(title);
if ((mDrawFlags & ONE_LINE) == 0) {
params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW, 1);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
params.addRule(RelativeLayout.LEFT_OF, 3);
TextView artist = new TextView(mContext);
artist.setPadding(mPadding, 0, 0, mPadding);
artist.setSingleLine();
artist.setTextSize(mSize);
artist.setId(2);
artist.setLayoutParams(params);
view.addView(artist);
}
if (mExpanderListener != null)
view.setupExpander(mMediaField, mExpanderListener);
}
updateText(position, (TextView)view.findViewById(1),(TextView)view.findViewById(2));
if (mExpanderListener != null)
view.findViewById(3).setTag(R.id.id, (int)getItemId(position));
updateView(position, view);
return view;
}

View File

@ -1,19 +1,19 @@
package org.kreed.vanilla;
import android.content.Context;
import android.widget.TextView;
public class AlbumAdapter extends AbstractAdapter {
public AlbumAdapter(Context context, Song[] allSongs)
{
super(context, Song.filter(allSongs, new Song.AlbumComparator()), 0, Song.FIELD_ALBUM);
super(context, Song.filter(allSongs, new Song.AlbumComparator()), MediaView.EXPANDER | MediaView.SECONDARY_LINE, Song.FIELD_ALBUM);
}
@Override
protected void updateText(int position, TextView upper, TextView lower)
protected void updateView(int position, MediaView view)
{
Song song = get(position);
upper.setText(song.album);
lower.setText(song.artist);
view.setPrimaryText(song.album);
view.setSecondaryText(song.artist);
view.setMediaId(song.albumId);
}
}

View File

@ -19,18 +19,18 @@
package org.kreed.vanilla;
import android.content.Context;
import android.widget.TextView;
public class ArtistAdapter extends AbstractAdapter {
public ArtistAdapter(Context context, Song[] allSongs)
{
super(context, Song.filter(allSongs, new Song.ArtistComparator()), ONE_LINE, Song.FIELD_ARTIST);
super(context, Song.filter(allSongs, new Song.ArtistComparator()), MediaView.EXPANDER, Song.FIELD_ARTIST);
}
@Override
protected void updateText(int position, TextView upper, TextView lower)
protected void updateView(int position, MediaView view)
{
Song song = get(position);
upper.setText(song.artist);
view.setPrimaryText(song.artist);
view.setMediaId(song.artistId);
}
}

View File

@ -0,0 +1,153 @@
/*
* Copyright (C) 2010 Christopher Eby <kreed@kreed.org>
*
* This file is part of Vanilla Music Player.
*
* Vanilla Music Player is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* Vanilla Music Player is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.kreed.vanilla;
import android.content.Context;
import android.graphics.Color;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
public class MediaView extends ViewGroup {
public static final int SECONDARY_LINE = 0x1;
public static final int EXPANDER = 0x2;
private TextView mPrimaryLine;
private TextView mSecondaryLine;
private ImageView mExpander;
private int mPadding;
public MediaView(Context context, int flags)
{
super(context);
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
float textSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 14, metrics);
mPadding = (int)textSize / 2;
mPrimaryLine = new TextView(context);
mPrimaryLine.setSingleLine();
mPrimaryLine.setTextColor(Color.WHITE);
mPrimaryLine.setTextSize(textSize);
addView(mPrimaryLine);
if ((flags & SECONDARY_LINE) != 0) {
mSecondaryLine = new TextView(context);
mSecondaryLine.setSingleLine();
mSecondaryLine.setTextSize(textSize);
addView(mSecondaryLine);
}
if ((flags & EXPANDER) != 0) {
mExpander = new ImageView(context);
mExpander.setPadding(mPadding * 2, mPadding, mPadding, mPadding);
mExpander.setImageResource(R.drawable.expander_arrow);
addView(mExpander);
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
int width = MeasureSpec.getSize(widthMeasureSpec);
widthMeasureSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.AT_MOST);
heightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
int expanderHeight = 0;
int textHeight = 4 * mPadding;
if (mExpander != null) {
mExpander.measure(widthMeasureSpec, heightMeasureSpec);
expanderHeight = mExpander.getMeasuredHeight();
}
if (mSecondaryLine != null) {
mSecondaryLine.measure(widthMeasureSpec, heightMeasureSpec);
textHeight = mSecondaryLine.getMeasuredHeight() + mPadding;
}
mPrimaryLine.measure(widthMeasureSpec, heightMeasureSpec);
textHeight += mPrimaryLine.getMeasuredHeight();
setMeasuredDimension(width, Math.max(expanderHeight, textHeight));
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom)
{
int width = right - left;
int height = bottom - top;
int textWidth;
int textHeight = height - 2 * mPadding;
int actualHeight;
if (mExpander != null) {
textWidth = width - mExpander.getMeasuredWidth();
mExpander.layout(textWidth, 0, width, height);
} else {
textWidth = width;
}
textWidth -= 2 * mPadding;
if (mSecondaryLine != null) {
textHeight = (textHeight - mPadding) / 2;
actualHeight = mSecondaryLine.getMeasuredHeight();
top = mPadding * 3 / 2 + textHeight + (textHeight - actualHeight) / 2;
mSecondaryLine.layout(mPadding, top, mPadding + textWidth, top + actualHeight);
}
actualHeight = mPrimaryLine.getMeasuredHeight();
top = mPadding + (textHeight - actualHeight) / 2;
mPrimaryLine.layout(mPadding, top, mPadding + textWidth, top + actualHeight);
}
public void setupExpander(int field, View.OnClickListener listener)
{
if (mExpander != null) {
mExpander.setTag(R.id.field, field);
mExpander.setOnClickListener(listener);
}
}
public void setPrimaryText(String text)
{
if (mPrimaryLine != null)
mPrimaryLine.setText(text);
}
public void setSecondaryText(String text)
{
if (mSecondaryLine != null)
mSecondaryLine.setText(text);
}
public void setMediaId(int id)
{
if (mExpander != null)
mExpander.setTag(R.id.id, id);
}
}

View File

@ -21,7 +21,6 @@ package org.kreed.vanilla;
import java.util.Arrays;
import android.content.Context;
import android.widget.TextView;
public class SongAdapter extends AbstractAdapter {
private static Song[] sort(Song[] songs)
@ -32,14 +31,14 @@ public class SongAdapter extends AbstractAdapter {
public SongAdapter(Context context, Song[] allSongs)
{
super(ContextApplication.getContext(), sort(allSongs), 0, Song.FIELD_TITLE);
super(ContextApplication.getContext(), sort(allSongs), MediaView.SECONDARY_LINE, Song.FIELD_TITLE);
}
@Override
protected void updateText(int position, TextView upper, TextView lower)
protected void updateView(int position, MediaView view)
{
Song song = get(position);
upper.setText(song.title);
lower.setText(song.artist);
view.setPrimaryText(song.title);
view.setSecondaryText(song.artist);
}
}