java.lang.IllegalStateException:当RecyclerView正在计算布局或滚动时,无法调用此方法

时间:2018-01-09 06:28:19

标签: android android-recyclerview

我正在尝试从listview中删除一个未启动特定文本的项目" Dev"。但是当我删除项目并通过notifydatasetChanged()刷新回收器列表时,应用程序崩溃了。这个问题已经被提出,我已经看到了所有解决方案,但我找不到合适的解决方案。

EvelistAdapter.java

     public class EvelistAdater extends RecyclerView.Adapter<EvelistAdater.ViewHolder> {
    Context cxt;
    ArrayList<Dbbean> adapterlist;
    int row_index;
    public EvelistAdater(Context cxt, ArrayList<Dbbean> list) {
        adapterlist=list;
        this.cxt=cxt;
        Log.d("LIst count",""+adapterlist.size());
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.scanlistadp, viewGroup, false);
        return  new ViewHolder(view);
    }

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

        if(adapterlist.get(position).blename.startsWith("Dev")) {
            holder.tv_country.setText(adapterlist.get(position).blename);

        }else{
            removeAt(position);
        }
    }

    @Override
    public int getItemCount() {
        return adapterlist.size();
    }
    public class ViewHolder extends RecyclerView.ViewHolder{
        TextView tv_country;
        RelativeLayout layout;
        ImageView setting;
        public ViewHolder(View view) {
            super(view);
            tv_country = (TextView)view.findViewById(R.id.tshirtname);
            setting = (ImageView) view.findViewById(R.id.setting);
            layout = (RelativeLayout)view.findViewById(R.id.layout);
        }
    }
    public void removeAt(int position) {
        adapterlist.remove(position);
        notifyDataSetChanged();
    }
}

logcat的

E/UncaughtException: java.lang.IllegalStateException: Cannot call this method while RecyclerView is computing a layout or scrolling
                                                                        at android.support.v7.widget.RecyclerView.assertNotInLayoutOrScroll(RecyclerView.java:2586)
                                                                        at android.support.v7.widget.RecyclerView$RecyclerViewDataObserver.onItemRangeChanged(RecyclerView.java:4951)
                                                                        at android.support.v7.widget.RecyclerView$AdapterDataObservable.notifyItemRangeChanged(RecyclerView.java:11371)
                                                                        at android.support.v7.widget.RecyclerView$AdapterDataObservable.notifyItemRangeChanged(RecyclerView.java:11362)
                                                                        at android.support.v7.widget.RecyclerView$Adapter.notifyItemChanged(RecyclerView.java:6650)
                                                                        at com.scanner.com.eve.EvelistAdater.removeAt(EvelistAdater.java:94)
                                                                        at com.scanner.com.eve.EvelistAdater.onBindViewHolder(EvelistAdater.java:44)
                                                                        at com.scanner.com.eve.EvelistAdater.onBindViewHolder(EvelistAdater.java:19)
                                                                        at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6354)
                                                                        at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6387)
                                                                        at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5343)
                                                                        at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5606)
                                                                        at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5448)
                                                                        at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5444)
                                                                        at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2224)
                                                                        at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1551)
                                                                        at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1511)
                                                                        at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:595)
                                                                        at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3600)
                                                                        at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3329)
                                                                        at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3867)
                                                                        at android.view.View.layout(View.java:16655)
                                                                        at android.view.ViewGroup.layout(ViewGroup.java:5438)
                                                                        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
                                                                        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
                                                                        at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
                                                                        at android.view.View.layout(View.java:16655)
                                                                        at android.view.ViewGroup.layout(ViewGroup.java:5438)
                                                                        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
                                                                        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
                                                                        at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
                                                                        at android.view.View.layout(View.java:16655)
                                                                        at android.view.ViewGroup.layout(ViewGroup.java:5438)
                                                                        at android.support.v4.widget.SwipeRefreshLayout.onLayout(SwipeRefreshLayout.java:636)
                                                                        at android.view.View.layout(View.java:16655)
                                                                        at android.view.ViewGroup.layout(ViewGroup.java:5438)
                                                                        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
                                                                        at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
                                                                        at android.view.View.layout(View.java:16655)
                                                                        at android.view.ViewGroup.layout(ViewGroup.java:5438)
                                                                        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
                                                                        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
                                                                        at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
                                                                        at android.view.View.layout(View.java:16655)
                                                                        at android.view.ViewGroup.layout(ViewGroup.java:5438)
                                                                        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
                                                                        at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
                                                                        at com.android.internal.policy.PhoneWindow$DecorView.onLayout(PhoneWindow.java:2895)
                                                                        at android.view.View.layout(View.java:16655)
                                                                        at android.view.ViewGroup.layout(ViewGroup.java:5438)
                                                                        at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2171)
                                                                        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1931)
                                                                        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
                                                                        at android.view.ViewR

3 个答案:

答案 0 :(得分:1)

请勿直接在removeItem内拨打onBindViewHolder。如果要删除项目,请在设置适配器之前执行此操作。

首先将过滤后的ArrayList<Dbbean> list提供给adpater。后来您可以在任何指定操作中从适配器中删除项目。
 在您不确定更改了哪个数据集之前,请勿使用notifyDataSetChanged()。使用 : notifyItemRemoved()已移除某个特定商品的位置。

答案 1 :(得分:1)

在传递给list

之前过滤Adapter中的数据
 ArrayList<Dbbean> list = yourlist; 

创建已过滤的list

  ArrayList<Dbbean> filteredList = new ArrayList<>();
  for(Dbbean dbean : list){
    if(dbean.blename.startsWith("Dev")
     filteredList.add(dbean);
  }

将过滤后的列表传递给适配器

EvelistAdapter adapter = new EvelistAdaptr(this,filteredList);
yourRecyclerView.setAdapter(adapter);

答案 2 :(得分:0)

这导致了问题,你只是强行改变onBindViewHolder,它现在再次绘制一些东西来重绘。

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

    if(adapterlist.get(position).blename.startsWith("Dev")) {
        holder.tv_country.setText(adapterlist.get(position).blename);

    }else{
        removeAt(position);
    }
}

public void removeAt(int position) {
    adapterlist.remove(position);
    notifyDataSetChanged();
}

建议更好地检查此情况,并在您要通过片段或活动通知适配器时删除这些项目。