我正在使用RecyclerView
listview来显示一些列表值。在此列表中,我有两个ImageView
和一个Textview
。当我快速滚动列表时,应用程序崩溃了。它发生在某些设备上。有些设备工作但不能顺利滚动。
这是我的适配器
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();
}
}
这是我的清单
错误消息
答案 0 :(得分:0)
确保在查看onViewRecycled
@Override
public void onViewRecycled(ItemViewHolder holder) {
holder.imageView.setImageBitmap(null);
}
滚动时,您的recyclerview很可能仍然不够平滑,因为您在主线程中同步加载图像。 但我认为它可以在某些时候防止崩溃。因此,我建议您使用像Picasso或Glide这样的图像加载程序库,以确保图像以异步方式加载,并使您在recyclerview方式中滚动更加顺畅。
@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);
}