3

嗨,我正在编写部分代码,我需要在网格视图上绘制 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 方法,我不知道为什么:/

4

1 回答 1

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......

于 2012-12-04T22:20:17.727 回答