RecyclerView crashes when “scrapped or attached views may not be recycled”

前端 未结 27 2626
灰色年华
灰色年华 2020-11-29 19:50

I\'m using a simple implementation of RecyclerView taken from the Android website using a StaggeredGridLayoutManager and I keep getting this error

相关标签:
27条回答
  • 2020-11-29 19:59

    While using slimfit sticky headers i encountered this error. It was caused because of setting wrong first position. I got the answer here

    public void onBindViewHolder(MainViewHolder holder, int position) {
    
      final View itemView = holder.itemView;
      final LayoutManager.LayoutParams params = LayoutManager.LayoutParams.from(itemView.getLayoutParams());
    
      params.setSlm(LinearSLM.ID);
      params.width = ViewGroup.LayoutParams.MATCH_PARENT;
      params.setFirstPosition(item.mSectionFirstPosition);
      itemView.setLayoutParams(params);
    
    }
    

    just make sure you are passing correct value for mSectionFirstPosition

    0 讨论(0)
  • 2020-11-29 20:03

    I solve this problem by removing parent.addView() in onCreateViewHolder

    This is my code

    public MyViewHolder onCreateViewwHolder(ViewGroup parent, int viewType)  {
        Button addButton = new Button(context);
        //parent.addView(addButton);
        return new MyViewHolder(addButton);
    }
    

    Function at android.support.v7.widget.RecyclerViewRecycler.recyclerViewHolderinternal() check whether my button has already a parent or not. Which if we add button to parent, it will be also assign RecyclerView to its mParent variable.

    0 讨论(0)
  • 2020-11-29 20:05

    this exception is not cause of

    android:animateLayoutChanges

    or

    android:focusableInTouchMode

    this final correct answer is just because you set a WRONG LayoutParams.

        nameLP = new LinearLayout.LayoutParams(context.getResources().getDisplayMetrics().widthPixels, LinearLayout.LayoutParams.WRAP_CONTENT);
        nameLP2 = new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, RecyclerView.LayoutParams.WRAP_CONTENT);
    

    the nameLP is OK. the nameLP2 occur the crash .bug is here.

    I try all of answers of this page. trust me.

    0 讨论(0)
  • 2020-11-29 20:06

    I've had to deal with this crash also and in my case it had nothing to do with android:animateLayoutChanges.

    The RecyclerView we were building had more than one type of views in it and some were having EditTexts in them. After a while we pinned the issue to being focus related. This bug happens while recycling EditTexts and one of them is focused.

    Naturally we tried clearing the focus when new data is being bound to a recycled view but that didn't work until android:focusableInTouchMode="true" is set on RecycleView. Actually that is the only change that was needed in the end for this issue to go away.

    0 讨论(0)
  • 2020-11-29 20:07

    While in my case it was removing animateOnLayoutChange from the recyclerView that fixed the crash, I still needed the ability to animate the layout changes within the viewHolder. In order to get this to work, the LinearLayout' in the view holder needs theanimateOnLayoutChange' to to true, but I needed to notifyItemChanged to the adapter. This then allowed both of the layoutTransition animations to kick off (for expanding and collapsing the viewHolder), and also avoids the scrapped exception. So yes, avoid putting the animateOnLayoutChange on the recylcerView and use the various notify methods to enable the default animations on view size changes.

    0 讨论(0)
  • 2020-11-29 20:08

    Remove android:animateLayoutChanges="true" from recycleview or set android:animateLayoutChanges="false"

    0 讨论(0)
提交回复
热议问题