水平回收视图中的图像会绕过
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了水平回收视图中的图像会绕过相关的知识,希望对你有一定的参考价值。
我试图用图像和一些文本等进行简单的水平回收视图。但由于某种原因,当我来回滚动时,图像会四处跳动并最终出现在错误的位置。
问题在于,适配器毫无疑问:
public class SponsoredAdvertsAdapter extends RecyclerView.Adapter<SponsoredAdvertsAdapter.SponsoredAdvertHolder> {
private Context context;
private List<CustomAdvert> adverts;
private boolean isBigScreen;
public SponsoredAdvertsAdapter(Context context, List<CustomAdvert> adverts) {
this.context = context;
this.adverts = adverts;
isBigScreen = ScreenUtil.isBigScreen(context);
}
@Override
public SponsoredAdvertsAdapter.SponsoredAdvertHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_search_popular_adverts_phone, null);
if (isBigScreen) {
layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_search_popular_adverts_tablet, null);
}
return new SponsoredAdvertHolder(layoutView);
}
@Override
public void onBindViewHolder(final SponsoredAdvertHolder holder, int position) {
final CustomAdvert advert = adverts.get(position);
String title = advert.getAdvert().getValidTextShort();
holder.title.setText(title);
holder.unreadBadge.setText(context.getString(R.string.customer_tilbudsaviser_unread_advert));
if (advert.isRead()) {
holder.unreadBadge.setVisibility(View.INVISIBLE);
} else {
holder.unreadBadge.setVisibility(View.VISIBLE);
}
if (holder.image != null) {
attachAdvertImage(holder.image, holder.imageView);
} else {
String logoUrl = ImageScaleUrlBuilder.getFixedWidthUrl(advert.getLogoUrl(), holder.imageView.getMeasuredWidth());
ImageLoaderHelper.loadImageFromUrl(logoUrl, new ImageLoaderHelper.ImageLoadedCallback() {
@Override
public void onBitmapLoaded(Bitmap bitmap) {
holder.image = bitmap;
attachAdvertImage(bitmap, holder.imageView);
}
});
}
AdvertActivity.startAdvertActivity(context, advert.getCustomer(), advert.getAdvert(), 0, null);
}
private void attachAdvertImage(final Bitmap image, final ImageView imageView) {
RunOnUIThread.post(new Runnable() {
@Override
public void run() {
imageView.setImageBitmap(image);
}
});
}
@Override
public int getItemCount() {
return adverts.size();
}
public class SponsoredAdvertHolder extends RecyclerView.ViewHolder {
private final View container;
private final ImageView imageView;
private final TextView title, unreadBadge;
private Bitmap image;
public SponsoredAdvertHolder(View itemView) {
super(itemView);
container = itemView.findViewById(R.id.item_search_popular_adverts_container);
imageView = (ImageView) itemView.findViewById(R.id.search_popular_advert_imageview);
title = (TextView) itemView.findViewById(R.id.search_popular_advert_title);
unreadBadge = (TextView) itemView.findViewById(R.id.unread_badge);
}
}
}
这是我将适配器设置为recyclerview的代码:
sponsoredAdvertsAdapter = new SponsoredAdvertsAdapter(getContext(), adverts);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false);
sponsoredAdvertsRecyclerView.setLayoutManager(linearLayoutManager);
sponsoredAdvertsRecyclerView.setAdapter(sponsoredAdvertsAdapter);
这是recyclerview(xml):
<LinearLayout
android:id="@+id/popular_searches_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1"
android:background="@color/white">
<android.support.v7.widget.RecyclerView
android:id="@+id/popular_adverts_searches_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
和项目的xml:¨
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="8dp"
android:id="@+id/item_search_popular_adverts_container">
<ImageView
android:id="@+id/search_popular_advert_imageview"
android:layout_width="136dp"
android:layout_height="170dp"
android:gravity="bottom"
android:scaleType="fitXY"
android:layout_marginBottom="10dp"
android:layout_gravity="center_horizontal"/>
<TextView
android:id="@+id/search_popular_advert_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textColor="@color/secondary_grey"
android:textSize="14sp"
android:text="Title"/>
<include
android:id="@+id/unread_badge"
layout="@layout/item_badge"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="3dp"
android:text="@string/customer_tilbudsaviser_unread_advert"
android:visibility="invisible" />
</LinearLayout>
我知道这对Recyclerviews来说是一个相当普遍的问题,但即使在查看其他解决方案之后,我似乎无法让我的解决方案起作用。
答案
我建议你使用像Glide(git link)这样的图像库来显示图像视图。它会正确缓存图像并显示。这可能会解决问题;)
通过替换此线来使用滑行
imageView.setImageBitmap(图像);
至
Glide
.with(myFragment)
.load(bitmap)
.centerCrop()
.crossFade()
.into(myImageView);
以上是关于水平回收视图中的图像会绕过的主要内容,如果未能解决你的问题,请参考以下文章