2

当我每次尝试使用回收的网格视图时都会出错,如果 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
4

2 回答 2

3

convertView是完全代表您的一个项目的视图GridView。如果那是 a TextView,它将是 a TextView,如果是整个布局,您将收到整个布局,依此类推。

那么你怎么知道和定义什么是“代表一个项目的视图”呢?很简单,它是您在何时创建的任何内容convertView == null,然后returngetView.

只需您收到一个使用过的物品,您只需对其进行修改以将其更新为适当的内容。因此,您应该使用类型转换来以View您想要的格式收到它。

像下面这样的代码将为您提供您想要的东西,而无需重做您不需要做的事情(也就是您不需要Views从 a重新添加 child convertView,只需要一个新视图):

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;
}
于 2012-07-23T01:57:11.790 回答
0

[添加]

此外,您有代码

image.setImageResource(mThumbIds[position]);
text.setText("This is a test");

lay.addView(image);
lay.addView(text);

return lay;

在 if 和 else 之外,这意味着您每次都在请求单元格的视图时尝试添加子视图。您实际上只需要添加视图以放置在例程的 if 部分中。

[ORIGINAL] convertView 很可能是您的父级,所有其他视图都是它的子级。在你的 else 子句中,你所做的只是将每个设置为相同的东西。convertView 怎么能同时成为三个完全不同的东西。

很可能需要:

text = (TextView) convertView.SomeTextViewSubToConvertView;
image = (ImageView) convertView.SomeImageViewSubToConvertView;
lay = (RelativeLayout) convertView.SomeRelativeLayoutSubToConvertView;
于 2012-07-23T01:35:40.560 回答