问题
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?
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);
Edit: 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.
回答1:
I think the problem lies in the way you add the item into the list.
try to change it to
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();
}
});
}
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.
来源:https://stackoverflow.com/questions/51581231/images-disappear-when-scrolling-up-and-down-and-spaces-increase-between-images-u