无法将视图添加到布局中的现有视图

时间:2019-02-23 18:58:38

标签: java android android-recyclerview android-linearlayout recycler-adapter

我无法在OnBindView函数的回收者视图中动态地将视图添加到项目中。我收到一个运行时错误,指出以下内容,并指向行holder.getQuestionNumbersContainer().addView(textView);

The specified child already has a parent. You must call removeView() on the child's parent first.

最终目标是扩大item_question_number的版式,掌握文本视图text_question_number并为其设置文本。完成后,我想将其添加到questions_container线性布局中。

我有一个recyclerview,其中每个项目都具有以下布局

<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.card.MaterialCardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardCornerRadius="@dimen/card_corner_radius"
    app:cardElevation="@dimen/card_elevation"
    app:cardUseCompatPadding="true">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp">

        <LinearLayout
            android:id="@+id/questions_container"
            android:layout_margin="5dp"
            android:padding="8dp"
            android:orientation="horizontal"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/divider" />

    </androidx.constraintlayout.widget.ConstraintLayout>

</com.google.android.material.card.MaterialCardView>

在bindView中,我正在执行以下操作

 @Override
    protected void bindView(Section item, DrawerQuestionItem holder, int position) {

        final View questionNumberView = mLayoutInflater.inflate(R.layout.item_question_number, holder.getQuestionNumbersContainer(), false);

        final int questions_count = item.getQuestions().size();
        for (int i = 0; i < questions_count; i++) {
            TextView textView = questionNumberView.findViewById(R.id.text_question_number);
            textView.setText(String.valueOf(i));
            holder.getQuestionNumbersContainer().addView(textView);
        }

}

item_question_number看起来像这样

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/root"
    android:layout_width="30dp"
    android:background="@drawable/circle"
    android:layout_height="30dp">

    <TextView
        android:id="@+id/text_question_number"
        android:padding="3dp"
        android:layout_gravity="center"
        android:textColor="@color/textPrimary"
        android:includeFontPadding="false"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:text="88" />

</FrameLayout>

1 个答案:

答案 0 :(得分:0)

我已经跟踪了您的代码,对于您的问题,简化的适配器应如下所示:

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {

private final Context mContext;
List<String> data;

public MyAdapter(Context context, List<String> mdata) {
    this.data = mdata;
    this.mContext = context;
}

@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    int layoutId = R.layout.rec_item;
    LayoutInflater inflater = LayoutInflater.from(mContext);
    View itemView = inflater.inflate(layoutId, parent, false);
    return new MyViewHolder(itemView);
}

@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
    LayoutInflater inflater = LayoutInflater.from(mContext);
    final int questions_count = 5;
    for (int i = 0; i < questions_count; i++) {
        View questionNumberView = inflater.inflate(R.layout.item_question_number, holder.container, false);
        TextView textView = questionNumberView.findViewById(R.id.text_question_number);
        textView.setText(String.valueOf(i));
        holder.container.addView(questionNumberView);
    }
}

@Override
public int getItemCount() {
    return data.size();
}

public static class MyViewHolder extends RecyclerView.ViewHolder {
    ViewGroup container;
    public MyViewHolder(View itemView) {
        super(itemView);
        container = itemView.findViewById(R.id.questions_container);
    }

}

}

其他项目的视图布局为:

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="5dp"
app:cardElevation="5dp"
app:cardUseCompatPadding="true">

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dp">

    <LinearLayout
        android:id="@+id/questions_container"
        android:layout_margin="5dp"
        android:padding="8dp"
        android:orientation="horizontal"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        />

</androidx.constraintlayout.widget.ConstraintLayout>

这对我有用。