Answer the question
In order to leave comments, you need to log in
Android: ListView - multicolored items?
Hello, is it possible to implement the following, and if possible, I will be grateful for a piece of code, if not, then at least some advice.
I want to make sure that when loading my ListView, each item is painted over in a certain color (for example, some kind of my gradient) and also change when clicked.
Well, there is a way to change the color of examples when you click, but how to make all items be created in different colors ..?
So far this is the status
import java.util.List;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
public class RowAdapter extends BaseAdapter
{
private List<Row> elements;
private Context context;
public RowAdapter(Context c, List<Row> rows) {
this.elements = rows;
this.context = c;
}
@Override
public int getCount() {
return elements.size();
}
@Override
public Object getItem(int position) {
return elements.get(position);
}
@Override
public long getItemId(int id) {
return id;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return elements.get(position);
}
}
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.util.DisplayMetrics;
import android.view.View;
public class Row extends View {
private DisplayMetrics displayMetrics;
public Row(Context context, DisplayMetrics displayMetrics) {
super(context);
this.displayMetrics = displayMetrics;
}
@Override
public void onDraw(Canvas canvas) {
Paint paint = new Paint();
int xPos = (canvas.getWidth() / 2);
int yPos = (int) ((canvas.getHeight() / 2) - ((paint.descent() + paint.ascent()) / 2));
paint.setColor(Color.GREEN);
paint.setTextSize(30);
canvas.drawText("Some Text", xPos, yPos, paint);
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(displayMetrics.widthPixels, 300);
}
}
Answer the question
In order to leave comments, you need to log in
PVOID asked a fair question - why store views in the adapter? This is absolutely not a rational use of resources. Basically, all you need to know is the type of the current sheet element. By this type, you can set the background of the desired color.
Example:
1) We create a static class (optionally static, you can stuff it into the load somewhere) with all the colors - let's say ListColors.
public class ListColors {
public static final Integer RED = 0;
public static final Integer GREEN = 1;
public static final Integer BLUE = 2;
public static final Integer YELLOW = 3;
public static final Integer PURPLE = 4;
}
class ListItem {
private Long id;
private Integer type;
public ListItem(Long id, Integer type){
this.id = id;
this.type = type;
}
public Long getId(){
return id;
}
public Integer getType(){
return type;
}
}
List<ListItem>
.class TestAdapter extends BaseAdapter implements ListAdapter {
private Context context;
private List<ListItem> items;
public TestAdapter(Context context, List<ListItem> items){
this.context = context;
this.items = items;
}
@Override
public int getCount() {
return items.size();
}
@Override
public ListItem getItem(int position) {
return items.get(position);
}
@Override
public long getItemId(int position) {
return items.get(position).getId();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ListItem item = getItem(position);
convertView = LayoutInflater.from(context).inflate(R.layout.list_row_item, null);
LinearLayout layout = (LinearLayout) convertView.findViewById(R.id.view_background);
Integer colorRes;
switch (item.getType())
{
default:
case ListColors.RED:
colorRes = context.getResources().getColor(R.id.red);
break;
case ListColors.GREEN:
colorRes = context.getResources().getColor(R.id.green);
break;
case ListColors.BLUE:
colorRes = context.getResources().getColor(R.id.blue);
break;
case ListColors.YELLOW:
colorRes = context.getResources().getColor(R.id.yellow);
break;
case ListColors.PURPLE:
colorRes = context.getResources().getColor(R.id.purple);
break;
}
layout.setBackgroundColor(colorRes);
return convertView;
}
}
Hint: BaseAdapter.getItemViewType(int position)
BTW, why are you storing views in the adapter?
And why not save information about the state of the line through settag / gettag?
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
if (convertView == null)
{
convertView = LayoutInflater.from(context).inflate(R.layout.list_row_item, null);
//первое место где каждой вьюшке можно задать свой цвет, но она будет
//переиспользоваться и появляться в разных местах при листаниии
}
int color = 0;
switch(position)
{
case 0:
color = 0xFFff0000;
case 1:
color = 0xFF00ff00;
case 2:
color = 0xFF0000ff;
}
//можно напридумывать миллион правил по которым будет генерить цвет так-то )
converView.setBackgroundColor(color);
return converView;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question