gpt4 book ai didi

Android GridView 回收问题

转载 作者:搜寻专家 更新时间:2023-11-01 09:06:23 27 4
gpt4 key购买 nike

当我每次尝试使用回收的 gridview 时,如果 convertView != null 那么我得到一个错误,这是我的源代码。它会在 text = (TextView) convertView; 处给我一个错误在 else 语句中。我真的迷失在这里,我会停止回收 View ,但它会占用大量内存和不稳定的滚动

$ 这里是 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

最佳答案

convertView 是完全代表您的 GridView 中的一个 项目的 View 。如果那是一个 TextView,它将是一个 TextView,如果它是一个完整的布局,您将收到整个布局等等。

那么您如何知道和定义什么是“表示一个项目的 View ”?很简单,它是您在 convertView == null 然后从 getView return 时创建的任何内容。

只是您收到了一件用过的元素,您只是对其进行修改以将其更新为适当的内容。因此,您应该利用类型转换来以您想要的格式获取您收到的此 View

像下面这样的代码会让你得到你想要的,而不需要重做你不需要做的事情(也就是你不需要从 convertView 重新添加子 Views >,只有一个新 View ):

public View getView(int position, View convertView, ViewGroup parent) {
RelativeLayout lay;
ImageView image;
TextView text;

if (convertView == null) {
// Setup your 'item view'
lay = new RelativeLayout(mContext);
image = new ImageView(mContext);
text = new TextView(mContext);

// Do all your customizing stuff (aka size, color, format, padding layout params)

lay.addView(image, 0);
lay.addView(text, 1);
} else {
lay = (RelativeLayout) convertView;
image = (ImageView) lay.getChildAt(0);
text = (TextView) lay.getChildAt(1);
}

// Set content for your image and text

return lay;
}

关于Android GridView 回收问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11605386/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com