Android网格视图回收问题

时间:2012-07-23 01:18:12

标签: android gridview android-viewpager

我每次尝试使用循环的gridview时出现错误,如果convertView!= null,那么我在这里得到一个错误是我的源代码。 它会在text =(TextView)convertView上给我一个错误;在其他的声明中。我真的迷失在这里,我只是停止回收视图,但后来它的内存和它的波涛汹涌的滚动

$ here是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

2 个答案:

答案 0 :(得分:3)

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

那你怎么知道和定义什么是“代表一个项目的视图”?很简单,它是您convertView == null创建的内容,然后是return中的getView

只是您收到一个已使用的项目,而您只是修改它以将其更新为适当的内容。因此,您应该使用类型转换来获得所需格式的View

如下所示的代码可以为您提供您想要的内容而无需重做您不需要做的事情(也就是说您不需要从Views重新添加子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;
}

答案 1 :(得分:0)

<强> [ADDITION]

此外,您有代码

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

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

return lay;
在if和else之外的

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

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

最有可能的是:

text = (TextView) convertView.SomeTextViewSubToConvertView;
image = (ImageView) convertView.SomeImageViewSubToConvertView;
lay = (RelativeLayout) convertView.SomeRelativeLayoutSubToConvertView;