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の1つのアイテムを完全に表すビューですGridView。それがTextViewTextViewの場合は、、レイアウト全体の場合は、レイアウト全体を受け取ります。

では、「1つのアイテムを表すビュー」とは何かをどのように知り、定義するのでしょうか。シンプルで、それはあなたがいつ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;
}
于 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 部分内に配置するビューを追加することだけです。

[オリジナル] ほとんどの場合、convertView は親であり、他のすべてのビューはそのサブです。あなたのelse句では、それぞれをSAME EXACT THINGに設定するだけです。convertView を一度に 3 つの完全に異なるものにするにはどうすればよいでしょうか。

ほとんどの場合、次のようにする必要があります。

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