I\'m using a simple implementation of RecyclerView
taken from the Android website using a StaggeredGridLayoutManager
and I keep getting this error
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
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.
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.
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 EditText
s in them. After a while we pinned the issue to being focus related. This bug happens while recycling EditText
s 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.
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 the
animateOnLayoutChange' 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.
Remove android:animateLayoutChanges="true"
from recycleview or set android:animateLayoutChanges="false"