java.lang.IllegalStateException:当comparelerView计算布局或滚动时,无法调用此方法notifydatasetChanged()

时间:2017-06-21 08:50:53

标签: android android-recyclerview notifydatasetchanged

为了在创建活动后调用API,我需要为“0”索引的RecyclerView项执行click事件。

在onBindviewHolder中我正在调用holder.view.performClick事件。但问题是我需要在onClick事件中调用notifydataSetChanged()方法来更新适配器。最初,它会自动点击位置“0”并调用notifydataSetChanged()方法并崩溃。 这个问题的解决方案是什么?请帮助。

public void onBindViewHolder(ViewHolder holder, int position) {
    if (position==0){
        holder.itemView.performClick();
    }

我在MainActivity.class中调用此接口

public void onRecyclerViewItemClick(@NonNull View view, @NonNull CalendarDTO calendarDTO, int position) {
    isClickedPosition=position;
    adapter.notifyDataSetChanged();
    Toast.makeText(this, calendarDTO.getDate(), Toast.LENGTH_SHORT).show();
}

请检查错误: -

FATAL EXCEPTION: main
Process: in.playin.calendarrecyclerview, PID: 4063
java.lang.IllegalStateException: Cannot call this method while RecyclerView is computing a layout or scrolling
    at android.support.v7.widget.RecyclerView.assertNotInLayoutOrScroll(RecyclerView.java:2575)
    at android.support.v7.widget.RecyclerView$RecyclerViewDataObserver.onChanged(RecyclerView.java:4932)
    at android.support.v7.widget.RecyclerView$AdapterDataObservable.notifyChanged(RecyclerView.java:11359)
    at android.support.v7.widget.RecyclerView$Adapter.notifyDataSetChanged(RecyclerView.java:6636)
    at in.playin.calendarrecyclerview.CalendarRecyclerViewAdapter$ViewHolder$1.onClick(CalendarRecyclerViewAdapter.java:115)
    at android.view.View.performClick(View.java:5207)
    at in.playin.calendarrecyclerview.CalendarRecyclerViewAdapter.onBindViewHolder(CalendarRecyclerViewAdapter.java:59)
    at in.playin.calendarrecyclerview.CalendarRecyclerViewAdapter.onBindViewHolder(CalendarRecyclerViewAdapter.java:22)
    at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6356)
    at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6389)
    at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5335)
    at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5598)
    at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5440)
    at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5436)
    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:3583)
    at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:3025)
    at android.view.View.measure(View.java:18809)
    at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
    at android.view.View.measure(View.java:18809)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5955)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
    at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
    at android.view.View.measure(View.java:18809)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5955)
    at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:393)
    at android.view.View.measure(View.java:18809)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5955)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
    at android.view.View.measure(View.java:18809)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5955)
    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
    at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
    at android.view.View.measure(View.java:18809)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5955)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
    at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2643)
    at android.view.View.measure(View.java:18809)
    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2112)
    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1228)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1464)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1119)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6060)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)

3 个答案:

答案 0 :(得分:0)

public void onBindViewHolder(ViewHolder holder, int position) {
    holder.itemView.setOnClickListener(null);
    if (position == 0) {
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 holder.itemView.performClick();
            }
        });
    }
}

答案 1 :(得分:0)

根据您的描述,您必须以适当的方式编写吐司的上下文,如果您在适配器中然后使用上下文,如果您处于片段或活动中,请根据您的上下文记下它

谢谢...

答案 2 :(得分:0)

试试这个

在OnBindViewHolder

public static  boolean isPerformClicked;
int performClickCount;

 @Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
    if (i==0){
        viewHolder.tv_android.setBackgroundColor(Color.RED);
        if (performClickCount==0){
            isPerformClicked = true;
            viewHolder.itemView.performClick();
        }else{
            viewHolder.tv_android.setBackgroundColor(Color.RED);
        }

    }else{
        isPerformClicked=false;
        performClickCount = performClickCount+1;
        viewHolder.tv_android.setBackgroundColor(Color.YELLOW);

    }
    if (i!=BlankFragment.selectedPosition){
        viewHolder.tv_android.setBackgroundColor(Color.YELLOW);
    }else{
        viewHolder.tv_android.setBackgroundColor(Color.RED);
    }

    viewHolder.tv_android.setText(android_versions.get(i).getAndroid_version_name());
    Picasso.with(context).load(android_versions.get(i).getAndroid_image_url()).resize(120, 60).into(viewHolder.img_android);
}

在持有人项目点击或项目

  @Override
public void onRecyclerViewItemClick(@NonNull View view, @NonNull AndroidVersion calendarDTO, int position) {
    selectedPosition=position;
    Toast.makeText(getContext(),String.valueOf(position), Toast.LENGTH_SHORT).show();
    if (!isPerformClicked){
        adapter.notifyDataSetChanged();
    }
}