Convert MediaView into a single view
This commit is contained in:
parent
35181cc141
commit
4acc55f25b
@ -62,6 +62,11 @@ public class MediaAdapter extends BaseAdapter implements Filterable {
|
||||
return true;
|
||||
}
|
||||
|
||||
public int getPrimaryField()
|
||||
{
|
||||
return mPrimaryField;
|
||||
}
|
||||
|
||||
public View getView(int position, View convertView, ViewGroup parent)
|
||||
{
|
||||
MediaView view = null;
|
||||
@ -70,20 +75,10 @@ public class MediaAdapter extends BaseAdapter implements Filterable {
|
||||
} catch (ClassCastException e) {
|
||||
}
|
||||
|
||||
if (view == null) {
|
||||
int flags = 0;
|
||||
if (mSecondaryField != -1)
|
||||
flags |= MediaView.SECONDARY_LINE;
|
||||
if (mExpanderListener != null)
|
||||
flags |= MediaView.EXPANDER;
|
||||
if (view == null)
|
||||
view = new MediaView(mContext, mPrimaryField, mSecondaryField, mExpanderListener != null);
|
||||
|
||||
view = new MediaView(mContext, flags);
|
||||
|
||||
if (mExpanderListener != null)
|
||||
view.setExpanderOnClickListener(mExpanderListener);
|
||||
}
|
||||
|
||||
view.updateMedia(get(position), mPrimaryField, mSecondaryField);
|
||||
view.updateMedia(get(position));
|
||||
|
||||
return view;
|
||||
}
|
||||
|
@ -19,138 +19,114 @@
|
||||
package org.kreed.vanilla;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.Paint;
|
||||
import android.util.DisplayMetrics;
|
||||
import android.util.TypedValue;
|
||||
import android.view.MotionEvent;
|
||||
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;
|
||||
public class MediaView extends View {
|
||||
private static float mTextSize = -1;
|
||||
private static Bitmap mExpander = null;
|
||||
|
||||
private TextView mPrimaryLine;
|
||||
private TextView mSecondaryLine;
|
||||
private ImageView mExpander;
|
||||
private SongData mData;
|
||||
private int mPrimaryField;
|
||||
private int mSecondaryField;
|
||||
private boolean mHasExpander;
|
||||
|
||||
private int mPadding;
|
||||
private boolean mExpanderPressed;
|
||||
|
||||
public MediaView(Context context, int flags)
|
||||
public MediaView(Context context, int primaryField, int secondaryField, boolean hasExpander)
|
||||
{
|
||||
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 (mExpander == null)
|
||||
mExpander = BitmapFactory.decodeResource(context.getResources(), R.drawable.expander_arrow);
|
||||
if (mTextSize == -1) {
|
||||
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
|
||||
mTextSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 14, metrics);
|
||||
}
|
||||
|
||||
if ((flags & EXPANDER) != 0) {
|
||||
mExpander = new ImageView(context);
|
||||
mExpander.setPadding(mPadding * 2, mPadding, mPadding, mPadding);
|
||||
mExpander.setImageResource(R.drawable.expander_arrow);
|
||||
addView(mExpander);
|
||||
}
|
||||
mPrimaryField = primaryField;
|
||||
mSecondaryField = secondaryField;
|
||||
mHasExpander = hasExpander;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
|
||||
{
|
||||
int width = MeasureSpec.getSize(widthMeasureSpec);
|
||||
int expanderHeight;
|
||||
int textHeight;
|
||||
|
||||
widthMeasureSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.AT_MOST);
|
||||
heightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
|
||||
if (mHasExpander)
|
||||
expanderHeight = mExpander.getHeight() + (int)mTextSize;
|
||||
else
|
||||
expanderHeight = 0;
|
||||
|
||||
int expanderHeight = 0;
|
||||
int textHeight = 4 * mPadding;
|
||||
if (mSecondaryField != -1)
|
||||
textHeight = (int)(7 * mTextSize / 2);
|
||||
else
|
||||
textHeight = (int)(2 * mTextSize);
|
||||
|
||||
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));
|
||||
setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec),
|
||||
Math.max(expanderHeight, textHeight));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onLayout(boolean changed, int left, int top, int right, int bottom)
|
||||
public void onDraw(Canvas canvas)
|
||||
{
|
||||
int width = right - left;
|
||||
int height = bottom - top;
|
||||
if (mData == null)
|
||||
return;
|
||||
|
||||
int textWidth;
|
||||
int textHeight = height - 2 * mPadding;
|
||||
int width = getWidth();
|
||||
int height = getHeight();
|
||||
float padding = mTextSize / 2;
|
||||
|
||||
int actualHeight;
|
||||
Paint paint = new Paint();
|
||||
paint.setTextSize(mTextSize);
|
||||
paint.setAntiAlias(true);
|
||||
|
||||
if (mExpander != null) {
|
||||
textWidth = width - mExpander.getMeasuredWidth();
|
||||
mExpander.layout(textWidth, 0, width, height);
|
||||
if (mHasExpander) {
|
||||
width -= padding * 3 + mExpander.getWidth();
|
||||
canvas.drawBitmap(mExpander, width + padding * 2, (height - mExpander.getHeight()) / 2, paint);
|
||||
}
|
||||
|
||||
canvas.clipRect(padding, 0, width - padding, height);
|
||||
|
||||
float allocatedHeight;
|
||||
|
||||
if (mSecondaryField != -1) {
|
||||
allocatedHeight = height / 2 - padding * 3 / 2;
|
||||
|
||||
paint.setColor(Color.GRAY);
|
||||
canvas.drawText(mData.getField(mSecondaryField), padding, height / 2 + padding / 2 + (allocatedHeight - mTextSize) / 2 - paint.ascent(), paint);
|
||||
} else {
|
||||
textWidth = width;
|
||||
allocatedHeight = height - padding * 2;
|
||||
}
|
||||
|
||||
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);
|
||||
paint.setColor(Color.WHITE);
|
||||
canvas.drawText(mData.getField(mPrimaryField), padding, padding + (allocatedHeight - mTextSize) / 2 - paint.ascent(), paint);
|
||||
}
|
||||
|
||||
public void setExpanderOnClickListener(View.OnClickListener listener)
|
||||
public void updateMedia(SongData data)
|
||||
{
|
||||
if (mExpander != null)
|
||||
mExpander.setOnClickListener(listener);
|
||||
mData = data;
|
||||
invalidate();
|
||||
}
|
||||
|
||||
public void updateMedia(SongData song, int primaryField, int secondaryField)
|
||||
public SongData getExpanderData()
|
||||
{
|
||||
if (mPrimaryLine != null)
|
||||
mPrimaryLine.setText(song.getField(primaryField));
|
||||
if (mSecondaryLine != null)
|
||||
mSecondaryLine.setText(song.getField(secondaryField));
|
||||
if (mExpander != null) {
|
||||
SongData.Field data = null;
|
||||
try {
|
||||
data = (SongData.Field)mExpander.getTag();
|
||||
} catch (ClassCastException e) {
|
||||
}
|
||||
return mExpanderPressed ? mData : null;
|
||||
}
|
||||
|
||||
if (data == null) {
|
||||
data = new SongData.Field(primaryField, null);
|
||||
mExpander.setTag(data);
|
||||
}
|
||||
|
||||
data.data = song;
|
||||
}
|
||||
@Override
|
||||
public boolean onTouchEvent(MotionEvent event)
|
||||
{
|
||||
mExpanderPressed = mHasExpander && event.getX() > getWidth() - mExpander.getWidth() - 3 * mTextSize / 2;
|
||||
return false;
|
||||
}
|
||||
}
|
@ -158,7 +158,20 @@ public class SongSelector extends TabActivity implements AdapterView.OnItemClick
|
||||
|
||||
public void onItemClick(AdapterView<?> list, View view, int pos, long id)
|
||||
{
|
||||
sendSongIntent(((MediaAdapter)list.getAdapter()).buildSongIntent(mDefaultAction, pos));
|
||||
MediaAdapter adapter = (MediaAdapter)list.getAdapter();
|
||||
SongData data = ((MediaView)view).getExpanderData();
|
||||
if (data != null) {
|
||||
int field = adapter.getPrimaryField();
|
||||
SongData.Field limiter = new SongData.Field(field, data);
|
||||
for (int i = field; i != 3; ++i) {
|
||||
MediaAdapter tabAdapter = getAdapter(i);
|
||||
tabAdapter.hideAll();
|
||||
tabAdapter.setLimiter(limiter);
|
||||
}
|
||||
mTabHost.setCurrentTab(field);
|
||||
} else {
|
||||
sendSongIntent(adapter.buildSongIntent(mDefaultAction, pos));
|
||||
}
|
||||
}
|
||||
|
||||
public void afterTextChanged(Editable editable)
|
||||
@ -227,10 +240,7 @@ public class SongSelector extends TabActivity implements AdapterView.OnItemClick
|
||||
} catch (ClassCastException e) {
|
||||
}
|
||||
|
||||
if (data == null)
|
||||
return;
|
||||
|
||||
if (view instanceof TextView) {
|
||||
if (data != null) {
|
||||
SongData.Field limiter = getAdapter(mTabHost.getCurrentTab()).getLimiter();
|
||||
int field = data.field - 1;
|
||||
limiter = limiter.field == 0 ? null : new SongData.Field(field, limiter.data);
|
||||
@ -241,14 +251,6 @@ public class SongSelector extends TabActivity implements AdapterView.OnItemClick
|
||||
adapter.setLimiter(limiter);
|
||||
}
|
||||
updateLimiterViews();
|
||||
} else {
|
||||
SongData.Field limiter = new SongData.Field(data);
|
||||
for (int i = data.field; i != 3; ++i) {
|
||||
MediaAdapter adapter = getAdapter(i);
|
||||
adapter.hideAll();
|
||||
adapter.setLimiter(limiter);
|
||||
}
|
||||
mTabHost.setCurrentTab(data.field);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user