当我每次尝试使用回收的网格视图时都会出错,如果 convertView != null 那么我得到一个错误,这是我的源代码。它会在 text = (TextView) convertView; 处给我一个错误。在 else 语句中。我真的迷路了,我会停止回收视图,但是它会占用大量内存并且滚动不连贯
$ 这里是 imageadapter.java
public View getView(int position, View convertView, ViewGroup parent) {
RelativeLayout lay;
ImageView image;
TextView text;
if (convertView == null) {
Log.d("height", "Width = " + width);
lay = new RelativeLayout(mContext);
image = new ImageView(mContext);
text = new TextView(mContext);
//text.setText("This is a test");
text.setTextSize(14);
text.setTextColor(Color.WHITE);
text.setGravity(Gravity.LEFT | Gravity.TOP);
text.setPadding(2, 2, 2, 2);
text.setBackgroundColor(Color.parseColor("#80000000"));
RelativeLayout.LayoutParams textLayout = new RelativeLayout.LayoutParams(
(int) Math.round(width / 2.0),
(int) Math.round(width / 8.3));
textLayout.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
text.setLayoutParams(textLayout);
MarginLayoutParams textMarginFix = (ViewGroup.MarginLayoutParams) text
.getLayoutParams();
textMarginFix.setMargins(0, 0, 0, (int) Math.round(width / 45.0));
text.setLayoutParams(textMarginFix);
image.setLayoutParams(new LayoutParams((int) Math
.round(width / 2.0), (int) Math.round(width / 2.0)));
image.setScaleType(ImageView.ScaleType.CENTER_CROP);
//image.setImageResource(mThumbIds[position]);
lay.setLayoutParams(new GridView.LayoutParams((int) Math
.round(width / 2.0), (int) Math.round(width / 2.0)));
lay.setBackgroundResource(R.drawable.shadowimage);
lay.setPadding(5, 5, 15, 15);
//lay.setId(mThumbIds[position]);
//lay.addView(image);
//lay.addView(text);
}
else
{
text = (TextView) convertView;
image = (ImageView) convertView;
lay = (RelativeLayout) convertView;
}
image.setImageResource(mThumbIds[position]);
text.setText("This is a test");
lay.addView(image);
lay.addView(text);
return lay;
}
$here is where i call the imageadapter from another class
@Override
public Object instantiateItem(View container, int position) {
View contentView;
switch (position) {
case 0:
LayoutInflater mInflater = LayoutInflater.from(mContext);
View contentView = mInflater.inflate(R.layout.image_grid_view, null);
Display display = mContext.getWindowManager().getDefaultDisplay();
final int width = display.getWidth();
int height = display.getHeight();
float scale = mContext.getResources().getDisplayMetrics().density;
GridView gridview = (GridView) contentView.findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(mContext, width, height, scale));
gridview.setFocusable(true);
gridview.requestFocus();
gridview.setOnItemClickListener(itemClickListener);
((ViewPager) container).addView(contentView, 0);
break;
...return contentView