0
我正在嘗試使用圖像和一些文本等進行簡單的水平回收視圖。 但是,由於某些原因,當我前後滾動時,圖像正在切換並在錯誤的地方結束。水平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的一個相當常見的問題,但即使在查看其他解決方案後,我似乎也無法讓我的解決方案正常工作。
任何幫助感激,
你嘗試到該項目的寬度和高度從'match_parent'到'wrap_parent'改變? –
@HoangNguyen是的,我只是試圖..沒有區別。但我不知道爲什麼會解決這個問題。你有什麼特別的理由提出這個建議? – Langkiller
啊,我的壞,對不起。我誤解你的問題。 –