用于交替行颜色的通用阵列适配器?
Generic Array Adapter for Alternate Row Color?
我正在尝试开发一个通用数组适配器 Class,我想从中继承奇数行和偶数行的行颜色 属性。我的通用适配器 Class 是
public class SpecialAdapter<T> extends ArrayAdapter<T> {
private int[] colors = new int[] { 0x30FF0000, 0x300000FF };
public SpecialAdapter(Context context, int resource, List<T> items) {
super(context, resource,items);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
int colorPos = position % colors.length;
view.setBackgroundColor(colors[colorPos]);
return view;
}
}
我想将其扩展到我的适配器 class 以更改行颜色。
public class MyArrayAdapter 扩展了 SpecialAdapter{..}
可能吗?目前它不显示任何颜色。我没有在我的适配器 class 中设置任何颜色。我只需要从基础 class 继承行颜色,这样我就可以将它用于不同的适配器 class.
这是我的适配器class
public MyArrayAdapter(Activity context, List<MyObject> list) {
super(context, R.layout.mylayout, list);
this.context = context;
this.list = list;
}
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
view = inflator.inflate(R.layout.mylayout, null);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.txtName = (TextView) view.findViewById(R.id.label);
view.setTag(viewHolder);
} else {
view = convertView;
((ViewHolder) view.getTag()).txtName.setTag(list.get(position));
}
ViewHolder holder = (ViewHolder) view.getTag();
holder.txtName.setText("Position " + position);
return view;
}
据我所知没有这种通用的方法。如果只有2种颜色可以用下面的
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
if (position % 2 == 1) {
view.setBackgroundColor(Color.BLUE); // you can add any color here
} else {
view.setBackgroundColor(Color.CYAN); // don't like cyan? add another color.
}
return view;
}
我按照 includeMe 的建议进行了一些调整,现在可以正常使用了。我可以将我的布局作为动态传递。
public class SpecialAdapter<T> extends ArrayAdapter<T> {
private int[] colors = new int[] { 0x30FF0000, 0x300000FF };
int resource;
Activity context;
List<T> items;
public SpecialAdapter(Activity context, int resource, List<T> items) {
super(context, resource, items);
this.resource = resource;
this.context = context;
this.items =items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// View view = super.getView(position, convertView, parent);
View view = context.getLayoutInflater().inflate(resource, null);
int colorPos = position % colors.length;
view.setBackgroundColor(colors[colorPos]);
return view;
}
}
并在我的 Adapter class getView 方法中添加了这一行。
view = super.getView(position, convertView, parent);
替换此行。
view = context.getLayoutInflater().inflate(R.layout.mylayout, null);
现在我不需要在每个适配器中保留颜色更改代码class。 Superclass 会处理的。
我正在尝试开发一个通用数组适配器 Class,我想从中继承奇数行和偶数行的行颜色 属性。我的通用适配器 Class 是
public class SpecialAdapter<T> extends ArrayAdapter<T> {
private int[] colors = new int[] { 0x30FF0000, 0x300000FF };
public SpecialAdapter(Context context, int resource, List<T> items) {
super(context, resource,items);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
int colorPos = position % colors.length;
view.setBackgroundColor(colors[colorPos]);
return view;
}
}
我想将其扩展到我的适配器 class 以更改行颜色。
public class MyArrayAdapter 扩展了 SpecialAdapter{..}
可能吗?目前它不显示任何颜色。我没有在我的适配器 class 中设置任何颜色。我只需要从基础 class 继承行颜色,这样我就可以将它用于不同的适配器 class.
这是我的适配器class
public MyArrayAdapter(Activity context, List<MyObject> list) {
super(context, R.layout.mylayout, list);
this.context = context;
this.list = list;
}
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
view = inflator.inflate(R.layout.mylayout, null);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.txtName = (TextView) view.findViewById(R.id.label);
view.setTag(viewHolder);
} else {
view = convertView;
((ViewHolder) view.getTag()).txtName.setTag(list.get(position));
}
ViewHolder holder = (ViewHolder) view.getTag();
holder.txtName.setText("Position " + position);
return view;
}
据我所知没有这种通用的方法。如果只有2种颜色可以用下面的
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
if (position % 2 == 1) {
view.setBackgroundColor(Color.BLUE); // you can add any color here
} else {
view.setBackgroundColor(Color.CYAN); // don't like cyan? add another color.
}
return view;
}
我按照 includeMe 的建议进行了一些调整,现在可以正常使用了。我可以将我的布局作为动态传递。
public class SpecialAdapter<T> extends ArrayAdapter<T> {
private int[] colors = new int[] { 0x30FF0000, 0x300000FF };
int resource;
Activity context;
List<T> items;
public SpecialAdapter(Activity context, int resource, List<T> items) {
super(context, resource, items);
this.resource = resource;
this.context = context;
this.items =items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// View view = super.getView(position, convertView, parent);
View view = context.getLayoutInflater().inflate(resource, null);
int colorPos = position % colors.length;
view.setBackgroundColor(colors[colorPos]);
return view;
}
}
并在我的 Adapter class getView 方法中添加了这一行。
view = super.getView(position, convertView, parent);
替换此行。
view = context.getLayoutInflater().inflate(R.layout.mylayout, null);
现在我不需要在每个适配器中保留颜色更改代码class。 Superclass 会处理的。