水平recyclerview中的图像左右转动
问题描述:
我正在尝试使用图像和一些文本等进行简单的水平回收视图。 但是,由于某些原因,当我前后滚动时,图像正在切换并在错误的地方结束。水平recyclerview中的图像左右转动
的问题在于,没有在适配器一个疑问:
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);
你尝试到该项目的宽度和高度从'match_parent'到'wrap_parent'改变? –
@HoangNguyen是的,我只是试图..没有区别。但我不知道为什么会解决这个问题。你有什么特别的理由提出这个建议? – Langkiller
啊,我的坏,对不起。我误解你的问题。 –