我有一个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 Objects填充的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;
}
}
如果我得到一个解决方案,这将是非常有帮助的。感谢。