Android RecyclerView 项目序列问题

标签 android android-recyclerview android-adapter listadapter

我是 Android 新手。我目前正在尝试构建一个具有 ListView 屏幕的应用程序,其中显示了书籍列表。在每个项目上,有一个从 URL 下载的图像和 2 个图标,其图像资源是根据 POJO 对象中设置的值设置的。

我的适配器代码如下:

    Context context = view.getContext();
    view.setLayoutManager(new LinearLayoutManager(context));
    view.setAdapter(new MySearchBooksItemRecyclerViewAdapter(SearchBooks.ITEMS, mListener, getActivity()));

    public void onBindViewHolder(final ViewHolder holder, int position) {
    Log.d(TAG, "onBindViewHolder: binding variables of object: " + position);
    Log.d(TAG, "onBindViewHolder: holder value: " + holder.mItem);
    Log.d(TAG, "onBindViewHolder: holder value: " + holder.mTitle);
    Log.d(TAG, "onBindViewHolder: holder value: " + holder.mISBN);

    holder.mItem = mValues.get(position);
    holder.mTitle.setText(mValues.get(position).getTitle());
    holder.mTitle.setTag(mValues.get(position).getId());
    holder.mISBN.setText(mValues.get(position).getIsbn());
    holder.mDesc.setText(mValues.get(position).getDesc());
    holder.mView.setOnClickListener(new ClickListItem());

    getImages(holder.mBookImage, mValues.get(position).getImage());

    if (bookList.get(mValues.get(position).getId()) != null) {
        holder.mIsFav.setImageResource(bookList.get(mValues.get(position).getId()).isBookmarked() ? R.drawable.ic_favorite_white_24dp : R.drawable.ic_favorite_border_white_24dp);
        holder.mIsDownloaded.setImageResource(new File(bookList.get(mValues.get(position).getId()).getSavedBookName()).exists() ? R.drawable.ic_offline_pin_white_24dp : R.drawable.ic_get_app_white_24dp);
    }

    Log.d(TAG, "onBindViewHolder: variables bound: " + mValues.get(position));
}

@Override
public int getItemCount() {
    Log.d(TAG, "getItemCount: item count: " + mValues.size());
    return mValues.size();
}

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:backgroundTint="@android:color/darker_gray"
    android:orientation="horizontal">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="5"
        android:background="@color/background_material_dark"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/bookImage"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:minHeight="100dp"

            android:layout_weight="2"
            android:src="@drawable/icon_img_tu" />

        <LinearLayout
            android:layout_weight="1"
            android:minHeight="40dp"
            android:layout_gravity="center"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/is_fav_image"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:src="@drawable/ic_favorite_border_white_24dp" />

            <ImageView
                android:id="@+id/is_downloaded_image"
                android:layout_weight="1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/background_material_dark"
                android:src="@drawable/ic_get_app_white_24dp" />

        </LinearLayout>
    </LinearLayout>

现在,当我滚动屏幕时,问题就来了。在第一次加载中,图标值和图像被完美加载。但是当我滚动并返回时,再次调用“onBindViewHolder”并使用了错误的随机值。从而在图像中显示错误的图标。

谁能帮我解决这个问题...??

除非需要,否则我不希望重新加载图像或刷新图标

最佳答案

您应该在检查 id 是否为 null 的地方添加 else 语句,在 else 子句中填充静态图像或任何占位符。

当您没有在 ImageView 中加载图像并且 ImageView 获取随机图像时,就会发生这种情况。 在您的情况下,您只有 if 语句,因此当 if 语句为 false 时,ImageView 不会加载任何图像。 Recyclerview 回收 View ,但我们必须确保填充 recyclerview 行中的所有值,否则将显示以前的图像/数据。

希望这对您有所帮助。如果它解决了你的问题,那就接受它。谢谢

关于Android RecyclerView 项目序列问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36607825/

相关文章:

android - 如何在 RectShape 或 OvalShape 中显示 Android Canvas ShapeDrawable 中的文本?

java - LibGDX 在实现一些 float 后在 Android 上崩溃

android - 在两个 recyclerview 之间拖放(再次)

android - RecyclerView : How to clear the recycled views from RecyclerView. 回收 View 池

Android 版本 4.4.4 的 java.lang.IllegalStateException

android - Recyclerview onCreateViewHolder 为每个项目调用

android - 如何让我的应用程序在华为 Mate 10 Pro 或其他 18 :9 ratio phones? 上全屏显示

android - 在三星设备上播放 mp4 文件时出现 PVMFErrContentInvalidForProgressivePlayback 错误

java - Android:传递允许 recyclerview 由特定字符串填充的变量

android - 所有 ViewHolders 同时创建