2013-09-07 4 views
0

Попытка построить простую сетку с представлениями, которые я создаю. Представления будут выглядеть домино это класс, который определяет их, что расширяет ПосмотретьКак раздуть сетку, полную пользовательских представлений?

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(); 
    } 

} 

А затем в адаптер массива я пытаюсь построить их как этот

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; 
    } 

} 

Но я не вижу ничего, я хотите программно установить высоту и ширину в адаптере массива. Что мне не хватает?

ответ

1
// Replace this piece of code see what happens. 

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); 
    convertView.setTag(domino); 
} else { 
    domino = (Domino) convertView.getTag(); 
} 
Смежные вопросы