无法将视图添加到布局中的现有视图
Unable to add views to an existing view in the layout
我无法在 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>
我已经跟踪了您的代码,针对您的问题的简化适配器应该如下所示:
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>
这对我有用。
我无法在 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>
我已经跟踪了您的代码,针对您的问题的简化适配器应该如下所示:
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>
这对我有用。