How do I inflate a Grid full of custom views?
Trying to build a simple grid with views I create. The views will look
like dominos this is the class that defines them that extends view
public class Domino extends View{
private Paint paint;
public Domino(Context context) {
super(context);
init();
}
public void init(){
paint = new Paint();
paint.setTextSize(12);
paint.setColor(0xFF668800);
paint.setStyle(Paint.Style.FILL);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
invalidate();
}
}
And then in an array adapter I try to build them like this
private class CustomAdapter extends ArrayAdapter<String> {
private Context mContext;
private int tileW, tileH;
private List<String> list = new ArrayList<String>();
public CustomAdapter(Context context, int textViewResourceId,
List<String> objects) {
super(context, textViewResourceId, objects);
this.mContext = context;
this.list = objects;
// we need to do some calculation to get accurate screen
dimensions if we're going fullscreen
DisplayMetrics displayMetrics = new DisplayMetrics();
((WindowManager)
getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(displayMetrics);
this.tileW = displayMetrics.widthPixels / 4;
this.tileH = tileW/2;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Domino domino;
if (convertView == null) {
// if it's not recycled, initialize some attributes
domino = new Domino(mContext);
domino.setLayoutParams(new GridView.LayoutParams(this.tileW,
this.tileH));
domino.measure(this.tileW, this.tileH);
}
else {
domino = (Domino) convertView;
}
String colorString = list.get(position);
int rid = 0;
// figure out what color we're going to use
if (colorString.equals("r")) {
rid = R.drawable.grid_red;
}
else if (colorString.equals("o")) {
rid = R.drawable.grid_orange;
}
else if (colorString.equals("y")) {
rid = R.drawable.grid_yellow;
}
else if (colorString.equals("g")) {
rid = R.drawable.grid_green;
}
else if (colorString.equals("b")) {
rid = R.drawable.grid_blue;
}
else if (colorString.equals("i")) {
rid = R.drawable.grid_indigo;
}
else if (colorString.equals("v")) {
rid = R.drawable.grid_violet;
}
else {
rid = R.color.black;
}
return domino;
}
}
But I dont see anything, I want to programmatically set the height and
width in the array adapter. What am i missing?
No comments:
Post a Comment