Android - RecyclerView 具有一个布局,多个 setVisibility [英] Android - RecyclerView with one layout, multiple setVisibility
问题描述
我有一个基本上多合一的布局,其中包含我的应用程序的主要提要所需的一切.所有可变项(图片、视频缩略图等)一开始都设置为GONE
,需要时设置为VISIBLE
.
I have a basically all in one layout which has everything needed for my app's main feed. All variable items (images, video thumbnails.. Etc.) are set to GONE
at first and set to VISIBLE
when it is needed.
有时会出现问题,可能是由于RecyclerView
的回收行为,应该是GONE
的项目在VISIBLE
错误的地方.
The problem is sometimes, might be due to RecyclerView
's recycling behavior, the item which is supposedto be GONE
is VISIBLE
in the wrong places.
示例:
第 1 项包含文本
第 2 项包含图像
第 3 项包含图片
我一直向下滚动到第 x 项,然后向上滚动,这就是我得到的:
I keep scrolling down to item no x, then scroll back up and here's what I get :
第 1 项包含第 x 项的图像,有时是第 3 项
Item no 1 contains Image from item no x, sometimes item no 3
第 2 项包含图像
第 3 项包含图片
我正在使用一个自定义的 ViewHolder,它扩展了 RecyclerView.ViewHolder
.CustomViewHolder
的目的是为了布局声明和初始化.
I'm using a custom ViewHolder which extends RecyclerView.ViewHolder
.
The purpose of the CustomViewHolder
is for layout declaration and initialization.
ProgressBar progressBar;
View viewDimmer;
RelativeLayout postListWrapper;
...
public ObjectViewHolder(View v) {
super(v);
progressBar = (ProgressBar)v.findViewById(R.id.post_inscroll_progressBar);
viewDimmer = (View)v.findViewById(R.id.post_inscroll_viewDimmer);
postListWrapper = (RelativeLayout)v.findViewById(R.id.post_inscroll_postListWrapper);
}
我如何加载图像的示例:
An example of how I load the image :
Picasso.with(context)
.load(youtubeThumbnailUrl)
.fit()
.centerCrop()
.into(
((ObjectViewHolder) holder).userPostYoutubeImage
);
如果没有从服务器获取 url,我已将每个可见性设置为 GONE
I've set each visibility to GONE
if no url is obtained from the server
((ObjectViewHolder) holder).userPostImageWrapper.setVisibility(View.GONE);
((ObjectViewHolder) holder).userPostYoutubeImageWrapper.setVisibility(View.GONE);
但不知何故,图像仍然在以前的项目上重复使用(是的,不仅是第 1 项).有时图像也在错误的 ImageView
中.图像 D 应该在 ImageView
D 中,但它在 ImageView
A 中.
But somehow the image is still reused on the previous items (yes, not only Item no 1). Sometimes image are also in the wrong ImageView
. Image D is supposed to be in ImageView
D, but it's in ImageView
A instead.
关于设置 RecyclerView
并顺利运行的任何指南?
Any guide for setting RecyclerView
up and going nicely?
如果我遗漏了什么,或者需要提供更多代码,请通知我 :D
If I miss anything, or need to supply more code, please do inform me :D
推荐答案
else
条件也需要加入.就像下面的例子.
You need to put the else
condition too. Like the example below.
// if no url is found from server
if(url == null){
((ObjectViewHolder) holder).userPostImageWrapper.setVisibility(View.GONE);
((ObjectViewHolder) holder).userPostYoutubeImageWrapper.setVisibility(View.GONE);
} else {
// Some url has found
((ObjectViewHolder) holder).userPostImageWrapper.setVisibility(View.VISIBLE);
((ObjectViewHolder) holder).userPostYoutubeImageWrapper.setVisibility(View.VISIBLE);
}
如果您在运行时设置它们的可见性,请对作为列表项的每个项目执行此操作.
Do this for each of the items you've got there as the list item in case of you're setting their visibilities in runtime.
这篇关于Android - RecyclerView 具有一个布局,多个 setVisibility的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!