RecyclerView和Glide弄乱了布局

时间:2016-10-05 19:40:38

标签: android-recyclerview android-glide

我遇到一个奇怪的问题,RecyclerView使用Glide动态加载图像。看起来像竞争条件,我看了这个线程,但它没有解决问题:

Recyclerview Adapter and Glide - same image every 4-5 rows

以下是截图和红色箭头显示图像故障的方式,注意用户名文本总是存在。

enter image description here

代码本身看起来很直接:

RV适配器:

@Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
    int viewType = getItemViewType(position);
    bindSubscribersCell((SubscribersListViewHolder) viewHolder, position);
}

绑定视图:

private void bindSubscribersCell(SubscribersListViewHolder holder, int position) {
    SubscribersListRowTypeUser subscriberRow = (SubscribersListRowTypeUser) mItems.get(position);
    String userFullName = SDKUserHelper.MakeFullName(subscriberRow.getUser());
    holder.getUsername().setText(userFullName);
    holder.getUsername().setVisibility(View.VISIBLE);

    CircleImageView userImageView = holder.getAvatarImage();
    Drawable defaultIcon = ContextCompat.getDrawable(
            userImageView.getContext(),
            R.drawable.avatar_guest);
    RecylerViewHelper.loadImageView(
            subscriberRow.getUser().getPhotoUrl(),
            userImageView,
            defaultIcon,
            true);
}

图像加载助手使用上面提到的线程中描述的相同技术:

public static void loadImageView(
        String imageUrl,
        ImageView imageView,
        Drawable drawable,
        boolean icon) {
    Context context = imageView.getContext();
    if (imageUrl != null && !imageUrl.isEmpty()) {
        if (icon) {
            Glide.with(context).using(CloudinaryHelper.getUrlLoaderPresetPOIIcon(context)).
                    load(imageUrl).
                    centerCrop().
                    crossFade().
                    into(imageView);
        } else {
            Glide.with(context).
                    load(imageUrl).
                    crossFade().
                    into(imageView);
        }
    } else {
        Glide.clear(imageView);
        imageView.setImageDrawable(drawable);
    }
}

单元格的内部布局:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical">

    <de.hdodenhof.circleimageview.CircleImageView
        android:id="@+id/user_avatar"
        android:layout_width="wrap_content"
        android:layout_height="@dimen/splash_icon_height"
        android:layout_centerVertical="true"
        android:src="@drawable/avatar_guest" />

    <TextView
        android:id="@+id/user_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginEnd="40dp"
        android:layout_toEndOf="@+id/user_avatar"
        android:ellipsize="end"
        android:gravity="center"
        android:visibility="gone"
        android:maxLines="1"
        android:text=""
        android:textSize="14sp" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_centerVertical="true"
        android:layout_marginEnd="20dp"
        android:src="@drawable/arrow_forward_gray" />

</RelativeLayout>

用于获取布局控件的包装类:

public class SubscribersListViewHolder extends RecyclerView.ViewHolder {
    final private CircleImageView mAvatarImage;
    final private TextView mUsername;
    final private View mViewHolder;

    public SubscribersListViewHolder(View itemView) {
        super(itemView);
        mViewHolder = itemView;
        mUsername = (TextView) itemView.findViewById(R.id.user_name);
        assert mUsername != null;
        mAvatarImage = (CircleImageView) itemView.findViewById(R.id.user_avatar);
        assert mAvatarImage != null;
    }

    public TextView getUsername() { return mUsername; }
    public CircleImageView getAvatarImage() { return mAvatarImage; }
    public View getViewHolder() { return mViewHolder; }
}

如果有人能够发现竞争状况,我真的很感激。

谢谢你!

1 个答案:

答案 0 :(得分:0)

修复方法是将图标的宽度和高度设置为固定值,而不是&#34; wrap_content&#34;制作一些固定的dp(在我的情况下为49dp)。换句话说:

错误(竞争条件)

{
    headers: [
        {
            Title: "ID",
            Type: "text"
        },
        {
            Title: "StudentName",
            Type: "text"
        },
        {
            Title: "CourseName",
            Type: "text"
        },
        {
            Title: "SecondarySchoolMark",
            Type: "text"
        }
    ],
    application: [
        {
            Title: "ID",
            Value: "12345"
        },
        {
            Title: "StudentName",
            Value: "A B"
        },
        {
            Title: "CourseName",
            Value: 'B C'
        }
        {
            Title: 'SecondarySchoolMark',
            Value: '80'
        }
    ]
}

正确:

android:layout_width="wrap_content"
android:layout_height="@dimen/splash_icon_height"

但问题现在成为原因?我不知道。