0

我有一个LevelActivity。它调用一个自定义视图,LevelView

   public class LevelActivity extends Activity{

        ArrayList<Tile> mTiles = new ArrayList<Tile>();
        public static Resources resources;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);


            resources = this.getResources();
            setContentView(new LevelView(this));
        }

    }

一个自定义的 LevelView 类——

LevelView 用于创建一个应该用 Tile 对象填充的 gridView。因此,创建了一个自定义适配器 TileAdapter 对象。

   public class LevelView extends View{

        Context mContext;
        Canvas mCanvas;
        ArrayList<Tile>mTiles = new ArrayList<Tile>();
        boolean drawGlow;
        View mSingleTileView;
        int mSingleTilePosition;
        TileAdapter mAdapter;

        public LevelView(Context context) {
            super(context);
            mContext = context;
            LayoutInflater GridLayout = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            GridView gridview = (GridView) GridLayout.inflate(R.layout.level_view, null);

            this.addTilesToLevel();
            mAdapter = new TileAdapter(mContext, mTiles);
            gridview.setAdapter(mAdapter);
            mAdapter.notifyDataSetChanged();
            gridview.setOnItemClickListener(new OnItemClickListener() {

                public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                    mSingleTileView = v;
                    mSingleTilePosition = position;
                    drawGlow = true;
                    Toast.makeText(mContext, "" + position, Toast.LENGTH_SHORT).show();
                    LevelView.this.invalidate();
                }
            });
        }

        void addTilesToLevel() {
            RedTile redTile = new RedTile();
            redTile.addColor();
            redTile.addTileImage();
            mTiles.add(redTile);
            mTiles.add(redTile);
            mTiles.add(redTile);
            mTiles.add(redTile);
        }
    }

TileAdapter 类--- TileAdapter 包含 getView() 方法。问题就在这里。没有调用此方法。

  public class TileAdapter extends BaseAdapter{
        String TAG = "TileAdapter";
        private Context mContext;
        ArrayList<Tile> mTiles;

        public TileAdapter(Context c,ArrayList<Tile> tiles) {
            super();
            mContext = c;
            mTiles = tiles;
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return mTiles.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub

            ImageView imageView;

            int tileCount = getCount();
            Log.i(TAG, "In getView");
            GridView gridView = (GridView)parent;

            gridView.setNumColumns((int)(tileCount/2));
            if(convertView == null) {
                imageView = new ImageView(mContext);
                int viewWidth = imageView.getWidth();
                int viewHeight = imageView.getHeight();

                imageView.setLayoutParams(new GridView.LayoutParams(100, 200));
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                imageView.setPadding(4, 4, 4, 4);

            } else {
                imageView = (ImageView) convertView;
            }
            imageView.setImageBitmap(mTiles.get(position).getTileImage());
            return imageView;

        }
    }

如果我能解决这个问题,那将是非常有帮助的。谢谢。

4

0 回答 0