我正在尝试在网格视图中显示一些图像。我的代码适用于 android 2.2 和 2.3,但不适用于 2.1
在 android 2.1 中,网格始终为空/不可见。
我用于网格视图的代码是这样的:
gridview = (GridView)findViewById(R.id.level_select);
gridview.setAdapter(new LevelImagesAdapter(this, levelsCount,
maxLevel, listener));
gridview.invalidate();
LevelImagesAdapter 是:
public class LevelImagesAdapter extends BaseAdapter {
private View.OnClickListener listener;
private Context mContext;
private int count;
private int maxLevel;
public LevelImagesAdapter(Context c, int maxLevel, int count, View.OnClickListener listener) {
this.mContext = c;
this.listener = listener;
this.count = count;
this.maxLevel = maxLevel;
}
@Override
public int getCount() {
return count;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Button view;
if (convertView == null) {
view = new Button(mContext);
view.setOnClickListener(listener);
view.setLayoutParams(new GridView.LayoutParams(GridView.LayoutParams.WRAP_CONTENT, GridView.LayoutParams.WRAP_CONTENT));
view.setPadding(5, 5, 5,5);
}
else {
view = (Button) convertView;
}
if (position <= maxLevel) {
view.setBackgroundResource(R.drawable.level_unlock);
view.setText((position + 1) + "");
view.setTextColor(Color.WHITE);
view.setTextSize(16.0f);
}
else {
view.setBackgroundResource(R.drawable.level_lock);
view.setText("");
}
return view;
}
}
据我所知,适配器已创建,但从未调用 getView 方法。
谢谢!