0

在我的RecylcerAdapter中,我想複製TextView複製視圖時出錯:指定的子項已有父項

我有一個LinearLayout其中包含一個TexView。我只想在LinearLayout內部動態複製TextView,以便我在LinearLayout內有10 TextView

<LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/tags_ll" 
     android:layout_marginLeft="20dp" 
     android:layout_marginRight="20dp" 
     android:layout_marginBottom="10dp" 
     android:orientation="horizontal" 
     android:gravity="right"> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/tags" 
      android:layout_gravity="center_vertical" 
      android:gravity="center" 
      android:text="عمومی" 
      android:layout_margin="5dp" 
      android:padding="5dp" 
      android:background="@drawable/border_with_background" 
      android:textSize="12dp" /> 
    </LinearLayout> 

MyViewHolder在RecyclerView:

public MyViewHolder(View view) { 
    super(view); 

    tags = (TextView) view.findViewById(R.id.tags); 
    tags_ll = (LinearLayout) view.findViewById(R.id.tags_ll); 

    for (int i = 0; i < 10; i++) { 
     TextView rowTextView = new TextView(view.getContext()); 
     //Clone the new textview, get all the properties of the existing textview 
     rowTextView = tags; 
     rowTextView.setText("This is row #" + i); 
     tags_ll.addView(rowTextView); 

    } 

} 

我得到以下錯誤:

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

我想現有的TextView的所有屬性將被複制到新創建的TextViews。

+0

這應該是什麼? 'rowTextView = tags;' –

+0

我想將已經存在的textview複製到這個新的文本視圖中。我希望將現有textview的所有屬性複製到新的textview中。 – SMahdiS

+0

這是java。這不是你如何從一個對象複製屬性到另一個對象 –

回答

1

你在做什麼是錯誤的,你不能複製TextView屬性rowTextView = tags;,你只需要替換rowTextView TextView與tags之一。

你需要設置你的代碼中新TextView屬性:

for (int i = 0; i < 10; i++) { 
    TextView rowTextView = new TextView(view.getContext()); 
    //set the new TextView properties from code 
    rowTextView.setText("This is row #" + i); 
    tags_ll.addView(rowTextView); 

} 

或者

您可以創建一個只包含具有所需屬性的TextView一個xml的佈局,並在它充氣你for循環。

如:

my_text_view.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_vertical" 
    android:gravity="center" 
    android:text="عمومی" 
    android:layout_margin="5dp" 
    android:padding="5dp" 
    android:background="@drawable/border_with_background" 
    android:textSize="12dp" /> 

在你ViewHolder

LayoutInflater inflater = LayoutInflater.from(context); 

for (int i = 0; i < 10; i++) { 

     TextView rowTextView= (TextView) inflater.inflate(R.layout.my_text_view, null, false); 
     rowTextView.setText("This is row #" + i); 
     tags_ll.addView(rowTextView); 

} 
1

你的做法是錯誤的 - 這條線:

rowTextView = tags; 

不會複製第一個TextView。這將使新的引用指向原始的textView,然後你會發現自己再次在佈局中添加原始的textView,導致錯誤。

你應該做的是創造新的TextView,並通過使用原始的TextView這樣添加它的屬性:

for (int i = 0; i < 10; i++) { 
    TextView rowTextView = new TextView(view.getContext()); 
    rowTextView.setText(tags.getText()); 
    rowTextView.setGravity(tags.getGravity()); 
    //set all other properties like this: rowTextView.setOther(tags.getOther())... 
    tags_ll.addView(rowTextView) 
} 
+0

謝謝你幫助理解了什麼是錯的。 – SMahdiS

3

原因這個錯誤是因爲,

tags = (TextView) view.findViewById(R.id.tags); 
TextView rowTextView = new TextView(view.getContext()); 
rowTextView = tags; 
tags_ll.addView(rowTextView); // tag.ll has already a child with id = tag 

當您複製該對象引用到新的TextView「rowTextView」現在這兩個標記和rowTextView引用了相同的對象,它已經存在於tag.ll LinearLayout中,這就是爲什麼它會拋出錯誤。

如果您的需要僅僅是用recyclerViewAdapter實現列表,而不是讓它變得非常複雜,那麼當您僅僅要爲該TextView變量分配新引用時,無需從上下文中獲取TextView。

如果您安裝了最新的AndroidStudio,您可以創建一個帶有列表的新片段,Android Studio將爲您提供一個很好的示例,說明如何使用RecyclerView適配器來填充List。或查看這個鏈接

http://hmkcode.com/android-simple-recyclerview-widget-example/

希望我幫助。

相關問題