13

これは私のgetView()です。

私のリストの最初の項目には常に画像が表示されないため、ここで何か間違ったことをしていることは明らかです。

ここでの問題はthe convertview、リサイクルしなければ問題ないからです。

私は何を間違っていますか??

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if(convertView==null) //IF I DELETE THIS IF EVERYTHING OK!!!
    convertView = inflater.inflate(R.layout.square, null);
    ImageView image = (ImageView) convertView.findViewById(R.id.background_image);
    if (data.get(position).isFollowed()) {
        int approprieteimage = getApproppreiateImage(data.get(position));
        Picasso.with(context).load(approprieteimage).centerCrop().error(R.drawable.no_image_available).transform(new TealTransformation(context)).fit().into(image);
    } else {
        int approprieteimage = getApproppreiateImage(data.get(position));
        Picasso.with(context).load(approprieteimage).centerCrop().error(R.drawable.no_image_available).fit().into(image);

    }
    AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams(width / 2, width / 2);
    convertView.setLayoutParams(layoutParams);
    return convertView;
}
4

6 に答える 6

5

こんにちはリサ・アンこれを試してください

ViewHolder h; //declare viewholder global

ビューホルダーでビューを取得

@Override
public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.square, parent, false);
            h = new ViewHolder();
            h.image = (ImageView) convertView.findViewById(R.id.background_image);
            convertView.setTag(h);
        }else{
            h = (ViewHolder)convertView.getTag();
        }
        //display in log and check if the the url of image is here and live.
        Log.e("checking","image file name"+data.get(position))
    if (data.get(position).isFollowed()) {
        int approprieteimage = getApproppreiateImage(data.get(position));
        Picasso.with(context).load(approprieteimage).centerCrop().error(R.drawable.no_image_available).transform(new TealTransformation(context)).fit().into(h.image);
    } else {
        int approprieteimage = getApproppreiateImage(data.get(position));
        Picasso.with(context).load(approprieteimage).centerCrop().error(R.drawable.no_image_available).fit().into(h.image);

    }
    AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams(width / 2, width / 2);
    convertView.setLayoutParams(layoutParams);

    return convertView;
}

ビューホルダー クラス

static class ViewHolder {
     ImageView  image;
  }

ListView のスクロール中にコードで findViewById() が頻繁に呼び出され、パフォーマンスが低下する可能性があります。アダプターがリサイクルのために膨張したビューを返した場合でも、要素を検索して更新する必要があります。findViewById() を繰り返し使用する方法は、「ビュー ホルダー」設計パターンを使用することです。

ViewHolder オブジェクトは、各コンポーネント ビューを Layout のタグ フィールド内に格納するため、繰り返し検索する必要なくすぐにアクセスできます。

ビューホルダーは、特に画像を表示するときに非常に便利なテクニックです。Android デベロッパー ドキュメント

その後、ログエラーを見せてください。

于 2015-07-21T05:22:52.313 に答える
1

このリンクで見つけたように: picasso issue ビューをリサイクルするときは、新しいリクエストをロードする前に cancelRequest を呼び出す必要があります。したがって、コードは次のようになります。

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if(convertView==null) //IF I DELETE THIS IF EVERYTHING OK!!!
    convertView = inflater.inflate(R.layout.square, null);
    ImageView image = (ImageView) convertView.findViewById(R.id.background_image);
    Picasso.with(context).cancelRequest(image); // cancel old request
    if (data.get(position).isFollowed()) {
        int approprieteimage = getApproppreiateImage(data.get(position));
        Picasso.with(context).load(approprieteimage).centerCrop().error(R.drawable.no_image_available).transform(new TealTransformation(context)).fit().into(image);
    } else {
        int approprieteimage = getApproppreiateImage(data.get(position));
        Picasso.with(context).load(approprieteimage).centerCrop().error(R.drawable.no_image_available).fit().into(image);

    }
    AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams(width / 2, width / 2);
    convertView.setLayoutParams(layoutParams);
    return convertView;
}
于 2015-07-23T07:53:11.660 に答える
0

convertView = inflater.inflate(R.layout.square,parent,false);

parentView を convertView に渡します。

于 2015-07-22T11:01:09.307 に答える
-1

これを試して

convertView = inflater.inflate(R.layout.square, parent, false);
于 2015-07-23T11:43:58.070 に答える