P
P
Pavel Peskov2016-02-21 13:49:51
Android
Pavel Peskov, 2016-02-21 13:49:51

How to make a loadable RecyclerView on android?

Hello, I have a problem with RecyclerView's loadable list. When loading, there is a duplication of images from the beginning of the list, and when scrolling up, all images go astray

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexey Perelygin, 2016-02-21
@pavelg777

And it is possible a code example from the adapter. In general, I suspect that you load images into the list asynchronously, and at the time of insertion you do not check in any way that this particular image must be inserted into the target ImageView. I check the url for the image. At the beginning of the download in onBind, just when I start to load the image, I put mImageView.setTag(R.id.target_url, url) and when I'm ready to insert it, I check for the urls to match.

P
Pavel Peskov, 2016-02-21
@pavelg777

I tried to clear the view before loading, and everything seems to work, but it doesn’t look very nice when the images reappear during the next scroll.
Here is the adapter code:

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

        public class ViewHolder extends RecyclerView.ViewHolder {
            public ImageView userPhoto;
            public TextView name;
            public TextView date;
            private ImageView image;
            public TextView text;
            public TextView likeCount;
            public TextView commCount;

            public ViewHolder(View v) {
                super(v);
                userPhoto = (ImageView)v.findViewById(R.id.userPhoto);
                name = (TextView)v.findViewById(R.id.name);
                date = (TextView)v.findViewById(R.id.date);
                image = (ImageView)v.findViewById(R.id.image);
                text = (TextView)v.findViewById(R.id.text);
                likeCount = (TextView)v.findViewById(R.id.likeCount);
                commCount = (TextView)v.findViewById(R.id.commentCount);
            }
        }

        @Override
        public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                       int viewType) {
            View v = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.photo_card_item, parent, false);
            ViewHolder vh = new ViewHolder(v);
            return vh;
        }

        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {

            PhotoLab photoLab = photoLabs.get(position);

            imageLoader.displayImage(photoLab.getProfilePhoto(), holder.userPhoto, options);
            holder.name.setText(photoLab.getfName() + " " + photoLab.getlName());

            Calendar c = Calendar.getInstance();
            c.setTimeInMillis(photoLab.getDate() * 1000);

            holder.date.setText(
                    c.get(Calendar.DAY_OF_MONTH) + "." +
                            (c.get(Calendar.MONTH) + 1) + "." +
                            c.get(Calendar.YEAR) + " " +
                            c.get(Calendar.HOUR) + ":" +
                            c.get(Calendar.MINUTE)
            );
            PhotoResource photoResource = photoLab.getPhotos().get(0);
            //if(holder.image.getDrawable() == null){
                holder.image.setImageResource(0); // обнуляю перед загрузкой
                imageLoader.getInstance().displayImage(photoResource.getPhoto(), holder.image); //это проблемная вьюшка
            //}
            holder.text.setText(photoResource.getText());
            holder.likeCount.setText(""+photoResource.getLikes());
            holder.commCount.setText(""+photoResource.getComments());

        }

        @Override
        public int getItemCount() {
            return photoLabs.size();
        }

    }

T
Tiberal, 2016-02-22
@Tiberal

replace UniversalImageLoader with glide for better display in RecyclerView

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question