您好我正在编写部分代码,我需要在网格视图上绘制81个元素。我基于BaseAdapter编写了我自己的自定义适配器。我的自定义视图的对象已创建,但不显示。
这是我的代码: 具有简单网格元素的XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
<pl.zapalka.sudoku.obj.SimpleFieldView
android:id="@+id/simpleFieldViewID"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</pl.zapalka.sudoku.obj.SimpleFieldView>
</LinearLayout>
自定义视图类:
public class SimpleFieldView extends View implements OnClickListener{
private static final String TAG = "SimpleFieldView";
private static SimpleFieldView _selectedView = null;
private static Paint _paint = new Paint();
private Rect _rectToDraw = new Rect(0, 0,100,100);
private int _digit = 0;
private boolean _locked = true;
public SimpleFieldView(Context context) {
super(context);
Log.d(TAG, "Creating object");
this.requestLayout();
this.invalidate();
// TODO Auto-generated constructor stub
}
public SimpleFieldView(Context context, AttributeSet attrs) {
super(context, attrs);
Log.d(TAG, "Creating object from XML");
this.requestLayout();
this.invalidate();
// TODO Auto-generated constructor stub
}
public void setDigit(int d){
this._digit = d;
}
public void setLocked(boolean l){
this._locked = l;
}
@Override
public void onClick(View v) {
if(v != SimpleFieldView._selectedView){
SimpleFieldView._selectedView = (SimpleFieldView) v;
}
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
Log.d(TAG, "Drawing element");
super.onDraw(canvas);
SimpleFieldView._paint.setColor(Color.BLACK);
SimpleFieldView._paint.setStrokeWidth(4);
canvas.drawRect(this._rectToDraw, SimpleFieldView._paint);
if(this == SimpleFieldView._selectedView){
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
// TODO Auto-generated method stub
super.onSizeChanged(w, h, oldw, oldh);
this._rectToDraw.set(0, 0, w, h);
}
}
自定义适配器:
public class SudokuGridAdapter extends BaseAdapter{
ArrayList<SimpleFieldInfo> _listOfElements;
Context _context;
public SudokuGridAdapter(Context context, ArrayList<SimpleFieldInfo> list){
this._listOfElements = list;
this._context = context;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return this._listOfElements.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return this._listOfElements.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
SimpleFieldInfo info = this._listOfElements.get(position);
if(convertView == null) {
LayoutInflater inflater = LayoutInflater.from(_context);
convertView = inflater.inflate(R.layout.simple_sudoku_field, null);
}
SimpleFieldView view = (SimpleFieldView)convertView.findViewById(R.id.simpleFieldViewID);
view.setDigit(info.getDigit());
view.setLocked(info.getLockedStatus());
view.invalidate();
return convertView;
}
}
活动代码的一部分:
this.setContentView(R.layout.sudoku_layout);
GridView v = (GridView) findViewById(R.id.sudokuGrid);
v.setNumColumns(9);
ArrayList<SimpleFieldInfo> list = new ArrayList<SimpleFieldInfo>();
for(int i=0; i<81; i++){
SimpleFieldInfo view = new SimpleFieldInfo(0, true);
list.add(view);
}
SudokuGridAdapter adapter = new SudokuGridAdapter(this, list);
v.setAdapter(adapter);
问题是OnDraw方法从不打电话,我不知道为什么:/
答案 0 :(得分:0)
我看到的一个值得注意的部分是你如何在适配器内膨胀视图。 我会尝试:
convertView = inflater.inflate(R.layout.simple_sudoku_field, parent, false);
而不是:
convertView = inflater.inflate(R.layout.simple_sudoku_field, null);
要检查的另一件事是您的自定义视图Width and Height
。
你只是wrap_content但在你的自定义视图onMeasure
中你没有指定你的宽度和高度...也许android把它当作0并且甚至不尝试绘制......
快速测试更改wrap_content to 20dp
或smth ...