Делаю раскрывающийся список, вот код. Предполагаю, что ошибка связана с LayoutInflater но не могу разобраться - как сделать правильно
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)