Unify ListView getView() code and add an expander icon to it
This commit is contained in:
parent
b767e4eca8
commit
03e11a5fee
BIN
res/drawable/expander_arrow.9.png
Normal file
BIN
res/drawable/expander_arrow.9.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.1 KiB |
@ -25,24 +25,37 @@ import java.util.regex.Matcher;
|
|||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.graphics.Color;
|
||||||
import android.util.DisplayMetrics;
|
import android.util.DisplayMetrics;
|
||||||
import android.util.TypedValue;
|
import android.util.TypedValue;
|
||||||
|
import android.view.View;
|
||||||
|
import android.view.ViewGroup;
|
||||||
import android.widget.BaseAdapter;
|
import android.widget.BaseAdapter;
|
||||||
import android.widget.Filter;
|
import android.widget.Filter;
|
||||||
import android.widget.Filterable;
|
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 abstract class AbstractAdapter extends BaseAdapter implements Filterable {
|
||||||
protected Context mContext;
|
public static final int ONE_LINE = 0x1;
|
||||||
|
public static final int NO_EXPANDER = 0x2;
|
||||||
|
|
||||||
|
private Context mContext;
|
||||||
private List<Song> mObjects;
|
private List<Song> mObjects;
|
||||||
private Song[] mAllObjects;
|
private Song[] mAllObjects;
|
||||||
private ArrayFilter mFilter;
|
private ArrayFilter mFilter;
|
||||||
protected float mSize;
|
private float mSize;
|
||||||
protected int mPadding;
|
private int mPadding;
|
||||||
|
private int mDrawFlags;
|
||||||
|
private int mFieldCount;
|
||||||
|
|
||||||
public AbstractAdapter(Context context, Song[] allObjects)
|
public AbstractAdapter(Context context, Song[] allObjects, int drawFlags, int numFields)
|
||||||
{
|
{
|
||||||
mContext = context;
|
mContext = context;
|
||||||
mAllObjects = allObjects;
|
mAllObjects = allObjects;
|
||||||
|
mDrawFlags = drawFlags;
|
||||||
|
mFieldCount = numFields;
|
||||||
|
|
||||||
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
|
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
|
||||||
mSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 14, metrics);
|
mSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 14, metrics);
|
||||||
@ -55,9 +68,64 @@ public abstract class AbstractAdapter extends BaseAdapter implements Filterable
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getAllowedFields()
|
protected abstract void updateText(int position, TextView upper, TextView lower);
|
||||||
|
|
||||||
|
public View getView(int position, View convertView, ViewGroup parent)
|
||||||
{
|
{
|
||||||
return 3;
|
RelativeLayout view = null;
|
||||||
|
try {
|
||||||
|
view = (RelativeLayout)convertView;
|
||||||
|
} catch (ClassCastException e) {
|
||||||
|
}
|
||||||
|
|
||||||
|
if (view == null) {
|
||||||
|
view = new RelativeLayout(mContext);
|
||||||
|
view.setPadding(mPadding, mPadding, mPadding, mPadding);
|
||||||
|
|
||||||
|
RelativeLayout.LayoutParams params;
|
||||||
|
|
||||||
|
if ((mDrawFlags & NO_EXPANDER) == 0) {
|
||||||
|
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.setImageResource(R.drawable.expander_arrow);
|
||||||
|
button.setId(3);
|
||||||
|
button.setLayoutParams(params);
|
||||||
|
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.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.setSingleLine();
|
||||||
|
artist.setTextSize(mSize);
|
||||||
|
artist.setId(2);
|
||||||
|
artist.setLayoutParams(params);
|
||||||
|
view.addView(artist);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
updateText(position, (TextView)view.findViewById(1),(TextView)view.findViewById(2));
|
||||||
|
|
||||||
|
return view;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Filter getFilter()
|
public Filter getFilter()
|
||||||
@ -98,7 +166,6 @@ public abstract class AbstractAdapter extends BaseAdapter implements Filterable
|
|||||||
for (int i = words.length; --i != -1; )
|
for (int i = words.length; --i != -1; )
|
||||||
matchers[i] = createMatcher(words[i]);
|
matchers[i] = createMatcher(words[i]);
|
||||||
|
|
||||||
int max = getAllowedFields();
|
|
||||||
int count = mAllObjects.length;
|
int count = mAllObjects.length;
|
||||||
ArrayList<Song> newValues = new ArrayList<Song>();
|
ArrayList<Song> newValues = new ArrayList<Song>();
|
||||||
newValues.ensureCapacity(count);
|
newValues.ensureCapacity(count);
|
||||||
@ -110,9 +177,9 @@ public abstract class AbstractAdapter extends BaseAdapter implements Filterable
|
|||||||
for (int j = matchers.length; --j != -1; ) {
|
for (int j = matchers.length; --j != -1; ) {
|
||||||
if (matchers[j].reset(song.artist).find())
|
if (matchers[j].reset(song.artist).find())
|
||||||
continue;
|
continue;
|
||||||
if (max > 1 && matchers[j].reset(song.album).find())
|
if (mFieldCount > 1 && matchers[j].reset(song.album).find())
|
||||||
continue;
|
continue;
|
||||||
if (max > 2 && matchers[j].reset(song.title).find())
|
if (mFieldCount > 2 && matchers[j].reset(song.title).find())
|
||||||
continue;
|
continue;
|
||||||
continue outer;
|
continue outer;
|
||||||
}
|
}
|
||||||
|
@ -1,54 +1,19 @@
|
|||||||
package org.kreed.vanilla;
|
package org.kreed.vanilla;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.graphics.Color;
|
|
||||||
import android.view.View;
|
|
||||||
import android.view.ViewGroup;
|
|
||||||
import android.widget.LinearLayout;
|
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
|
|
||||||
public class AlbumAdapter extends AbstractAdapter {
|
public class AlbumAdapter extends AbstractAdapter {
|
||||||
public AlbumAdapter(Context context, Song[] allSongs)
|
public AlbumAdapter(Context context, Song[] allSongs)
|
||||||
{
|
{
|
||||||
super(context, Song.filter(allSongs, new Song.AlbumComparator()));
|
super(context, Song.filter(allSongs, new Song.AlbumComparator()), 0, 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getAllowedFields()
|
protected void updateText(int position, TextView upper, TextView lower)
|
||||||
{
|
{
|
||||||
return 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
public View getView(int position, View convertView, ViewGroup parent)
|
|
||||||
{
|
|
||||||
LinearLayout view = null;
|
|
||||||
try {
|
|
||||||
view = (LinearLayout)convertView;
|
|
||||||
} catch (ClassCastException e) {
|
|
||||||
}
|
|
||||||
|
|
||||||
if (view == null) {
|
|
||||||
view = new LinearLayout(mContext);
|
|
||||||
view.setOrientation(LinearLayout.VERTICAL);
|
|
||||||
view.setPadding(mPadding, mPadding, mPadding, mPadding);
|
|
||||||
|
|
||||||
TextView title = new TextView(mContext);
|
|
||||||
title.setSingleLine();
|
|
||||||
title.setTextColor(Color.WHITE);
|
|
||||||
title.setTextSize(mSize);
|
|
||||||
title.setId(0);
|
|
||||||
view.addView(title);
|
|
||||||
|
|
||||||
TextView artist = new TextView(mContext);
|
|
||||||
artist.setSingleLine();
|
|
||||||
artist.setTextSize(mSize);
|
|
||||||
artist.setId(1);
|
|
||||||
view.addView(artist);
|
|
||||||
}
|
|
||||||
|
|
||||||
Song song = get(position);
|
Song song = get(position);
|
||||||
((TextView)view.findViewById(0)).setText(song.album);
|
upper.setText(song.album);
|
||||||
((TextView)view.findViewById(1)).setText(song.artist);
|
lower.setText(song.artist);
|
||||||
return view;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -19,40 +19,18 @@
|
|||||||
package org.kreed.vanilla;
|
package org.kreed.vanilla;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.graphics.Color;
|
|
||||||
import android.view.View;
|
|
||||||
import android.view.ViewGroup;
|
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
|
|
||||||
public class ArtistAdapter extends AbstractAdapter {
|
public class ArtistAdapter extends AbstractAdapter {
|
||||||
public ArtistAdapter(Context context, Song[] allSongs)
|
public ArtistAdapter(Context context, Song[] allSongs)
|
||||||
{
|
{
|
||||||
super(context, Song.filter(allSongs, new Song.ArtistComparator()));
|
super(context, Song.filter(allSongs, new Song.ArtistComparator()), ONE_LINE, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getAllowedFields()
|
protected void updateText(int position, TextView upper, TextView lower)
|
||||||
{
|
{
|
||||||
return 1;
|
Song song = get(position);
|
||||||
}
|
upper.setText(song.artist);
|
||||||
|
|
||||||
public View getView(int position, View convertView, ViewGroup parent)
|
|
||||||
{
|
|
||||||
TextView view = null;
|
|
||||||
try {
|
|
||||||
view = (TextView)convertView;
|
|
||||||
} catch (ClassCastException e) {
|
|
||||||
}
|
|
||||||
|
|
||||||
if (view == null) {
|
|
||||||
view = new TextView(mContext);
|
|
||||||
view.setPadding(mPadding, mPadding, mPadding, mPadding);
|
|
||||||
view.setSingleLine();
|
|
||||||
view.setTextColor(Color.WHITE);
|
|
||||||
view.setTextSize(mSize);
|
|
||||||
}
|
|
||||||
|
|
||||||
view.setText(get(position).artist);
|
|
||||||
return view;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -21,10 +21,6 @@ package org.kreed.vanilla;
|
|||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.graphics.Color;
|
|
||||||
import android.view.View;
|
|
||||||
import android.view.ViewGroup;
|
|
||||||
import android.widget.LinearLayout;
|
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
|
|
||||||
public class SongAdapter extends AbstractAdapter {
|
public class SongAdapter extends AbstractAdapter {
|
||||||
@ -36,39 +32,14 @@ public class SongAdapter extends AbstractAdapter {
|
|||||||
|
|
||||||
public SongAdapter(Context context, Song[] allSongs)
|
public SongAdapter(Context context, Song[] allSongs)
|
||||||
{
|
{
|
||||||
super(ContextApplication.getContext(), sort(allSongs));
|
super(ContextApplication.getContext(), sort(allSongs), NO_EXPANDER, 3);
|
||||||
}
|
}
|
||||||
|
|
||||||
public View getView(int position, View convertView, ViewGroup parent)
|
@Override
|
||||||
|
protected void updateText(int position, TextView upper, TextView lower)
|
||||||
{
|
{
|
||||||
LinearLayout view = null;
|
|
||||||
try {
|
|
||||||
view = (LinearLayout)convertView;
|
|
||||||
} catch (ClassCastException e) {
|
|
||||||
}
|
|
||||||
|
|
||||||
if (view == null) {
|
|
||||||
view = new LinearLayout(mContext);
|
|
||||||
view.setOrientation(LinearLayout.VERTICAL);
|
|
||||||
view.setPadding(mPadding, mPadding, mPadding, mPadding);
|
|
||||||
|
|
||||||
TextView title = new TextView(mContext);
|
|
||||||
title.setSingleLine();
|
|
||||||
title.setTextColor(Color.WHITE);
|
|
||||||
title.setTextSize(mSize);
|
|
||||||
title.setId(0);
|
|
||||||
view.addView(title);
|
|
||||||
|
|
||||||
TextView artist = new TextView(mContext);
|
|
||||||
artist.setSingleLine();
|
|
||||||
artist.setTextSize(mSize);
|
|
||||||
artist.setId(1);
|
|
||||||
view.addView(artist);
|
|
||||||
}
|
|
||||||
|
|
||||||
Song song = get(position);
|
Song song = get(position);
|
||||||
((TextView)view.findViewById(0)).setText(song.title);
|
upper.setText(song.title);
|
||||||
((TextView)view.findViewById(1)).setText(song.artist);
|
lower.setText(song.artist);
|
||||||
return view;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user