Images disappear when scrolling up and down and spaces increase between images using Glide and RecyclerView

*爱你&永不变心* 提交于 2020-01-03 15:59:00

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!