2012-03-28 168 views
4

我有一個應用程序,點擊後,顯示新聞內容的活動。我想在底部顯示註釋,這些註釋是在異步任務中動態加載的。以編程方式添加視圖(線性)佈局(即滾動視圖內)

一種方法是使用ListView和自定義的ArrayAdapter,但是,我不得不把ListView放在一個ScrollView中,這是一個問題,即使我手動重寫ListView的高度。它顯示一個列表項目,以及下一個列表項目的一部分。

另一種方法是將LinearLayout定義爲註釋的持有者,而不是膨脹在自己的xml文件中定義的另一個LinearLayouts,填充它的內容,附加到持有者的視圖。從程序的角度來看,它工作正常,只是它在新聞內容的下方推薦評論。就好像,它會在內容od news(內容重疊)下面創建另一個可滾動的評論視圖。

是否有任何其他方式來做到這一點,與列表無關,或者我如何使其他方法工作。

從保持新聞內容的XML佈局相關的代碼片段是:

<LinearLayout> //global layout 
    <LinearLayout> 
     //views that hold title, date, content 
    </LinearLayout>  

     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/commentsHolder" 
      android:orientation="vertical"> 

     <View 
      android:layout_width="fill_parent" 
      android:layout_height="2dp" 
      android:background="#ed1b24"/> 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="fill_parent" 
      android:text="KOMENTARI" 
      android:layout_marginLeft="5dp"/> 
     <View 
      android:layout_width="fill_parent" 
      android:layout_height="1dp" 
      android:background="#ed1b24"/> 

     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="0dip" 
      android:orientation="vertical" 
      android:id="@+id/comments_holder_view" 
      android:layout_weight="1"> 
     </LinearLayout> 


    </LinearLayout> 

</LinearLayout> 

,並對應於一個註釋代碼:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/comments_holder_item" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:orientation="vertical" 
android:padding="5dp"> 

<TextView 
    android:id="@+id/comment_content" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="@drawable/comments_back_details" 
    android:text="Ovde ide tekst komentara, koji se, naravno, dinamicki dodaje."/> 

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 

    <TextView 
     android:id="@+id/comment_author" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     style="@style/categoryStyle" 
     /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text=" | " 
     style="@style/categoryStyle" 
     /> 

    <TextView 
     android:id="@+id/comment_pubdate" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     style="@style/categoryStyle"/> 

</LinearLayout> 

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="1dp" 
    android:orientation="horizontal" 
    android:layout_margin="5dp" 
    android:background="#d1d1d1"> 
</LinearLayout> 

非常感謝您的任何回覆。這對我來說非常重要。

回答

2

我不是很確定你如何膨脹你的comments,但這裏是我如何做到這一點。

聲明的評論Layout作爲LinearLayout,並給它一個id,然後得到那個LinearLayout參考:

LinearLayout commentsLayout=(LinearLayout)findViewById(R.id.commentsLayoutId); 

,然後只需添加子Views到此佈局:

commentsLayout.addView(newComment); 
+0

通常你會使用'getLayoutInflater()。inflate(R.layout.comment,null);'從你的活動 – njzk2 2012-03-28 14:05:13

+0

我完全像你發佈的Ovidiu,它工作正常,但不知何故,線性佈局(ID爲comments_holder_vie w)持有的評論是在其他佈局下。如果我靜態地(手動)包括幾個評論佈局,一切都會顯示正常。看起來問題出現在動態添加這些視圖時。 Thanx回覆,但。 – Dimmy3 2012-03-28 14:09:13

+0

請發表一些代碼,說明如何創建添加的視圖(註釋)? – 2012-03-28 14:16:51

相關問題