Answer the question
In order to leave comments, you need to log in
Error when adding child view to RecyclerView, how to fix?
I am making a drop down list, here is the code. I assume that the error is related to LayoutInflater but I can not figure out how to do it right
public class CompRVAdapter extends RecyclerView.Adapter<CompRVAdapter.ViewHolder> {
CardView cardView;
@NonNull
@Override
public CompRVAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.fragment_comp, parent, false);
View child_view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.fragment_child_1, parent, true);
return new ViewHolder(view, child_view);
}
@Override
public void onBindViewHolder(@NonNull CompRVAdapter.ViewHolder holder, int position) {
}
@Override
public int getItemCount() {
return 15;
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public ViewHolder(View itemView, View childView) {
super(itemView);
cardView = (CardView) itemView.findViewById(R.id.cv1);
CardView childCardView = (CardView) childView.findViewById(R.id.child_cv);
LinearLayout linearLayout_childItems = (LinearLayout) itemView.findViewById(R.id.child_linear_layout);
for (int indexView = 0; indexView < 3; indexView++) {
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
linearLayout_childItems.addView(childCardView, layoutParams);
}
}
@Override
public void onClick(View view) {
}
}
}
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
at android.view.ViewGroup.addViewInner(ViewGroup.java:4417)
at android.view.ViewGroup.addView(ViewGroup.java:4258)
at android.view.ViewGroup.addView(ViewGroup.java:4230)
at ru.human_factors.characterist.adapters.CompRVAdapter$ViewHolder.<init>(CompRVAdapter.java:44)
at ru.human_factors.characterist.adapters.CompRVAdapter.onCreateViewHolder(CompRVAdapter.java:22)
at ru.human_factors.characterist.adapters.CompRVAdapter.onCreateViewHolder(CompRVAdapter.java:13)
at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:6685)
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5869)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5752)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5748)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2232)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1559)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1519)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:614)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3812)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3529)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:4082)
at android.view.View.layout(View.java:17637)
Answer the question
In order to leave comments, you need to log in
inflate(R.layout.fragment_comp, parent, false);
What kind of obscurantism is going on in the constructor of the holder? In the cycle, the same View is added, of course it will fall. You don't have to do this, do it all in the markup at once.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question