当使用Glide和RecyclerView进行上下滚动时,图像消失,并且图像之间的间隔增加 [英] Images disappear when scrolling up and down and spaces increase between images using Glide and RecyclerView
问题描述
我有一个RecyclerView
来显示我的图像,我使用Glide从Firebase Storage的数据库中的URLs
中加载图像.
I have a RecyclerView
to display my images and I use Glide to load images from their URLs
from my database in Firebase Storage.
问题是,当我上下滚动时,图像会重新加载,有时会消失,并且每个图像之间的间距会增加.我该如何阻止呢?
The problem is, when I scroll up and down, images reload and sometimes disappear and spaces between each image increase. How can I stop this?
我的RecyclerViewAdapter
My RecyclerViewAdapter
@Override
public myViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View row = LayoutInflater.from(parent.getContext()).inflate(R.layout.image_layout,parent,false);
myViewHolder myViewHolder = new myViewHolder(row);
return myViewHolder;
}
@Override
public void onBindViewHolder(final myViewHolder holder, int position) {
final int x = position;
imageClass = myClassImageList.get(position);
Glide.with(context)
.load(imageClass.getName().trim())
.diskCacheStrategy(DiskCacheStrategy.ALL)
.fitCenter()
.into(holder.imageView);
holder.imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new MyAsyncTask().execute(myClassImageList.get(x).getName());
}
});
}
以及我如何将列表发送到适配器
And how I send my List to Adapter
for(int i=0;i<imageNames.size();i++) {
StorageReference ref = storageReference.child("filee/" + imageNames.get(i));
ref.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
String imageURL = uri.toString();
ImageClass imageClass = new ImageClass();
imageClass.setName(imageURL);
list.add(imageClass);
recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
Toast.makeText(MainActivity.this, "Downloaded", Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
Toast.makeText(MainActivity.this, "Not Downloaded", Toast.LENGTH_SHORT).show();
}
});
}
adapter = new ImageRecyclerAdapter(MainActivity.this,list);
recyclerView.setAdapter(adapter);
编辑:我通过删除imageLayout中的LinearLayout解决了图像之间的空间问题.
I solved the space problem between images by deleting the LinearLayout in the imageLayout.
现在我的问题是,当我上下滚动时图像会重新加载.
Now my problem is that images reload when I scroll up and down.
推荐答案
我认为问题出在将项目添加到列表中的方式上.
I think the problem lies in the way you add the item into the list.
尝试将其更改为
adapter = new ImageRecyclerAdapter(MainActivity.this,list);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
for(int i=0;i<imageNames.size();i++) {
StorageReference ref = storageReference.child("filee/" + imageNames.get(i));
ref.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
String imageURL = uri.toString();
ImageClass imageClass = new ImageClass();
imageClass.setName(imageURL);
list.add(imageClass);
adapter.notifyDataSetChanged();
Toast.makeText(MainActivity.this, "Downloaded", Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
Toast.makeText(MainActivity.this, "Not Downloaded", Toast.LENGTH_SHORT).show();
}
});
}
,getDownloadUrl()是否按调用顺序返回结果?如果否,则图像可能会以随机顺序加载到列表中.
and is the getDownloadUrl() returns the result in the sequence they are called? If no, then it is likely that images loads with random sequence on the list.
这篇关于当使用Glide和RecyclerView进行上下滚动时,图像消失,并且图像之间的间隔增加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!