本文介绍了尝试从recyclerview删除项目时,RecyclerView正在计算布局或滚动时无法调用此方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试从recyclerview中删除我的商品,但是我总是遇到错误
I am Trying to remove my item from recyclerview, but i always getting error
我正在使用通知数据集已更改,我能解决这个问题吗?
i am using notify datasetchanged, can i solve this?
public class AdapterIntransit extends RecyclerView.Adapter<AdapterIntransit.ViewHolder> {
private Context context;
List<DataIntransit> data;
public AdapterIntransit(Context context, List<DataIntransit> data) {
this.context = context;
this.data = data;
}
@Override
public AdapterIntransit.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardintransit, parent, false);
return new ViewHolder(itemView);
}
@Override
public void onBindViewHolder(AdapterIntransit.ViewHolder holder, int position) {
if (data.get(position).getJml1() - data.get(position).getJml2() <= 0) {
data.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position, getItemCount());
notifyDataSetChanged();
} else {
holder.kode.setText(data.get(position).getKode());
holder.nama.setText(data.get(position).getNama());
holder.jumlah.setText(String.valueOf(data.get(position).getJml1() - data.get(position).getJml2()));
}
}
@Override
public int getItemCount() {
return data.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
TextView kode, nama, jumlah;
public ViewHolder(View itemView) {
super(itemView);
kode = (TextView)itemView.findViewById(R.id.kode);
nama = (TextView)itemView.findViewById(R.id.nama);
jumlah = (TextView)itemView.findViewById(R.id.jumlah);
}
}
}
推荐答案
以下答案对我有用
Below answer worked for me
这只是问题的workaround
.
通常在您在background thread
上调用notifyDataSetChanged()
时发生.因此,只需将通知移至UI thread
This usually occurs when you are calling notifyDataSetChanged()
on the background thread
. So just move notify to UI thread
dialeecyclerView.post(new Runnable()
{
@Override
public void run() {
myadapter.notifyDataSetChanged();
}
});
更多信息,如果需要,您可以在UI
线程上运行它
For more you can run it on UI
thread, if you needed
runOnUiThread(new Runnable(){
public void run() {
// UI code goes here
}
});
这篇关于尝试从recyclerview删除项目时,RecyclerView正在计算布局或滚动时无法调用此方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!