使用GridView自定义View的正确方法是什么?

时间:2014-12-30 19:20:34

标签: android android-canvas android-gridview

忍受我,我是Android开发人员的新手。我试图制作一个简单的应用程序,其中9张卡排列在3x3网格中。为此,我决定使用GridView布局以及每张卡的自定义视图。我主要基于GridView guide

这就是我所拥有的:

main.xml中

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/grid_cards"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:numColumns="3"
   android:stretchMode="columnWidth"
   android:gravity="center"
   />

MainActivity.java

public class MainActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        GridView grid_cards = (GridView) findViewById(R.id.grid_cards);
        grid_cards.setAdapter(new CardAdapter(this));

        grid_cards.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v,
                                    int position, long id) {
                Toast.makeText(MainActivity.this, "position = " + position,
                               Toast.LENGTH_SHORT).show();
            }
        });
    }
}

CardAdapter.java

public class CardAdapter extends BaseAdapter {
    private Context mContext;
    private int mSize;

    public CardAdapter(Context c) {
        mContext = c;
        mSize = 9;
    }

    public int getCount() {
        return mSize;
    }

    public Object getItem(int position) {
        return null; // XXX
    }

    public long getItemId(int position) {
        return 0; // XXX
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        CardView cardView;
        if (convertView == null) {  // if it's not recycled, initialize some attributes
            cardView = new CardView(mContext);
            cardView.setBackgroundColor(Color.WHITE);
        } else {
            cardView = (CardView) convertView;
        }

        // force redraw. XXX: truly required?
        cardView.invalidate();

        return cardView;
    }
}

CardView.java

public class CardView extends View {
    Paint paint = new Paint();

    public CardView(Context context) {
        super(context);
    }

    @Override
    public void onDraw(Canvas canvas) {
        paint.setColor(Color.RED);
        paint.setStyle(Paint.Style.FILL);
        paint.setStrokeWidth(1);
        canvas.drawRect(0, 0,
                        canvas.getWidth()-1,
                        canvas.getHeight()-1, paint);
    }
}

我所获得的并不是什么。该应用程序不会崩溃,但没有任何卡被绘制。添加一些Log.i()次调用似乎表明甚至没有调用onDraw()的{​​{1}}。我做错了什么?

谢谢!

1 个答案:

答案 0 :(得分:0)

您的错误是您从未调用过super.onDraw(画布)

   @Override
    public void onDraw(Canvas canvas) {
       super.onDraw(canvas); //<== this line is needed to begin drawing on the canvas
        paint.setColor(Color.RED);
        paint.setStyle(Paint.Style.FILL);
        paint.setStrokeWidth(1);
        canvas.drawRect(0, 0,
                        canvas.getWidth()-1,
                        canvas.getHeight()-1, paint);
    }

您需要使用setMinimumWidth()和setMinimumHeight()

显式设置派生的View维度