Android - RecyclerView 加载后冻结
Android - RecyclerView freeze after load
我有一个 RecyclerView,我通过检测 RecyclerView 的末尾从我的数据库中十个十个地加载项目。
我的问题:加载项目后,当我滚动时,RecyclerView 在 1-2 秒内冻结。
我认为问题来自每个项目中的 ImageView。
每次加载后,最后一个列表会像这样添加到适配器中:arrayAdapter.list.addAll(questions);
然后是 arrayAdapter.notifyDataSetChanged();
我的适配器:
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
final Question question = list.get(position);
holder.tvOwner.setText("Question posée par " + question.ownerUsername);
holder.tvQuestion.setText(question.question);
if (question.image != null) {
holder.questionImage.setImageBitmap(question.image);
holder.questionImage.setVisibility(View.VISIBLE);
holder.questionImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
NavigationMenu nav = (NavigationMenu) context;
nav.showPictureFullScreen(question.image);
}
});
} else {
holder.questionImage.setImageBitmap(null);
holder.questionImage.setVisibility(View.GONE);
}
感谢 Pavan
public class SetImageBitmap extends AsyncTask<Void, Void, Bitmap> {
Question q;
ImageView iv;
public SetImageBitmap(Question q, ImageView iv) {
this.q = q;
this.iv = iv;
}
@Override
protected Bitmap doInBackground(Void... params) {
return q.image;
}
@Override
protected void onPostExecute(Bitmap bitmap) {
iv.setImageBitmap(bitmap);
iv.setVisibility(View.VISIBLE);
}
}
我有一个 RecyclerView,我通过检测 RecyclerView 的末尾从我的数据库中十个十个地加载项目。
我的问题:加载项目后,当我滚动时,RecyclerView 在 1-2 秒内冻结。
我认为问题来自每个项目中的 ImageView。
每次加载后,最后一个列表会像这样添加到适配器中:arrayAdapter.list.addAll(questions);
然后是 arrayAdapter.notifyDataSetChanged();
我的适配器:
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
final Question question = list.get(position);
holder.tvOwner.setText("Question posée par " + question.ownerUsername);
holder.tvQuestion.setText(question.question);
if (question.image != null) {
holder.questionImage.setImageBitmap(question.image);
holder.questionImage.setVisibility(View.VISIBLE);
holder.questionImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
NavigationMenu nav = (NavigationMenu) context;
nav.showPictureFullScreen(question.image);
}
});
} else {
holder.questionImage.setImageBitmap(null);
holder.questionImage.setVisibility(View.GONE);
}
感谢 Pavan
public class SetImageBitmap extends AsyncTask<Void, Void, Bitmap> {
Question q;
ImageView iv;
public SetImageBitmap(Question q, ImageView iv) {
this.q = q;
this.iv = iv;
}
@Override
protected Bitmap doInBackground(Void... params) {
return q.image;
}
@Override
protected void onPostExecute(Bitmap bitmap) {
iv.setImageBitmap(bitmap);
iv.setVisibility(View.VISIBLE);
}
}