【发布时间】:2016-04-13 19:27:13
【问题描述】:
我是 Android 新手。我目前正在尝试构建一个具有列表视图屏幕的应用程序,其中显示书籍列表。在每个项目上,都有一个从 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 是否为空的地方添加 else 语句,在 else 子句中填充静态图像,希望这会有所帮助。
-
我怀疑这会有什么帮助...我会尝试一下...如果有帮助,那么我会告诉你...直到那时任何其他见解也会有帮助跨度>
-
哇...令人惊讶地以某种方式起作用...我想了解为什么会发生这种情况...请您提交一个答案,并详细说明为什么之前会出现问题
-
是的,我添加的答案可以帮助您理解。
标签: android android-recyclerview android-adapter listadapter