0

I'm using RecyclerView listview to show some list values.In this list I have two ImageView and one Textview. When I quickly scroll the list, app gets crashed. It's happened on some devices.Some devices it's worked but not smoothly scroll.

This is my adapter

  public class MYAdapter extends RecyclerView.Adapter<MYAdapter.ItemViewHolder> {
        private ArrayList<Data_Model> mItems;
        private ClickListener clicklistener = null;
        private Context context;
        public RVAdapter(Context context,ArrayList<Data_Model> arrayList) {
            this.context = context;
            this.mItems = arrayList;
     }
        public class ItemViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

            private TextView mTextView;
            private ImageView imageview;
            private ImageView mType;

            public ItemViewHolder(View itemView) {
                super(itemView);

                itemView.setOnClickListener(this);

                mTextView = (TextView) itemView.findViewById(R.id.list_item);
                imageview = (ImageView) itemView.findViewById(R.id.image);
                mType = (ImageView) itemView.findViewById(R.id.mtype);
            }
            @Override
            public void onClick(View v) {

                if (clicklistener != null) {
                    clicklistener.itemClicked(v, getAdapterPosition());
                }
            }
        }
        public void setClickListener(ClickListener listener) {
            this.clicklistener = listener;
        }

        @Override
        public void onBindViewHolder(ItemViewHolder itemViewHolder, int i) {
            itemViewHolder.mTextView.setText(mItems.get(i).getTitle());

            BitmapFactory.Options bmOptions = new BitmapFactory.Options();
            Bitmap bitmap = BitmapFactory.decodeFile(mItems.get(i).getImage(),bmOptions);

            itemViewHolder.imageview.setImageBitmap(bitmap);

            {
                    Bitmap bitmaps = BitmapFactory.decodeResource(context.getResources(),R.drawable.music);
            itemViewHolder.mType.setImageBitmap(bitmaps);
            }
        @Override
        public ItemViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_row, viewGroup, false);
            ItemViewHolder itemViewHolder = new ItemViewHolder(view);
            return itemViewHolder;
        }
        @Override
        public int getItemCount() {
            return mItems.size();
        }
    }

this is my list

enter image description here

Error Message

enter image description here

musica
  • 1,373
  • 3
  • 15
  • 34
vindy
  • 23
  • 7

1 Answers1

0

make sure to recyler image when view onViewRecycled

@Override
public void onViewRecycled(ItemViewHolder holder) {
    holder.imageView.setImageBitmap(null);
}

your recyclerview most likely still not smooth enough when scrolled, since you load image synchronously in main thread. but I think it can prevent crash at some point. so, I suggest you to use image loader library like Picasso or Glide to make sure image is loaded asynchronously and make scrolling in your recyclerview way more smoother.

@Override
public void onBindViewHolder(ItemViewHolder itemViewHolder, int i) {
    itemViewHolder.mTextView.setText(mItems.get(i).getTitle());
    Picasso.with(itemViewHolder.imageview.getContext()).load(mItems.get(i).getImage()).into(itemViewHolder.imageView);
    itemViewHolder.imageview.setImageBitmap(bitmap);
}
hakim
  • 3,819
  • 3
  • 21
  • 25