Listview 中的 Picasso 和 Holders 具有可变高度

Picasso and Holders in Listview with variable height

我正在使用 listView,其中我的项目使用以下 xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <com.makeramen.roundedimageview.RoundedImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/background_imageView" />


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <!-- Content -->
    </LinearLayout>

</FrameLayout>

适配器

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

    if (convertView == null) {
        convertView = _inflater.inflate(R.layout.mylayout, null);
        cell = new Cell(convertView);
        convertView.setTag(cell);
    } else {
        cell = (Cell) convertView.getTag();
    }

    // ... Some stuff with other views ...
    Picasso.with(_context).load("http://...").fit().centerCrop().into(cell.backgroundImageView);

    return convertView;
}

static class Cell {
    @Bind(R.id.background_imageView) ImageView backgroundImageView;
    // Other views

    public Cell(View view){
        ButterKnife.bind(this, view);
    }
}

这是我滚动时的结果:

看起来 Picasso 正在使用旧 Holder.imageView 的高度调整图像大小。 当我不使用 Holders 时,我没有这个问题。

你有什么想法吗?

看起来将 Picasso 调用包装到 cell.backgroundImageView.post() 中就可以了:

cell.backgroundImageView.post(new Runnable() {
                @Override
                public void run() {
                    Picasso.with(_context).load("http://...").fit().centerCrop().into(cell.backgroundImageView);
                }
            });

一旦视图 rendered/resized 就会调用 Picasso,并且可以对 imageView 应用正确的度量。