Programmatically add Views inside a (Linear)Layout (that is inside a ScrollView)

江枫思渺然 提交于 2019-12-07 08:14:14

问题


I have an app that, after some clicking, shows activity with news contents. I want at a bottom of that to show comments, which are dynamically loaded in async task.

One approach is to use ListView and custom ArrayAdapter, but, i would have to put ListView inside a ScrollView, and it is is a problem, even if i manually override ListView's height. It shows one list item, and one part of the following one.

The other approach is to define LinearLayout as a holder for comments, than inflate another LinearLayouts defined in it's own xml file, populate it's contents, attach to holder's view. It works fine, from program's point of view, except it pushes comments below contents of the news. It's like, it creates another scrollable view of comments underneath contents od news (contents overlap it).

Is there any other way of doing this that has nothing to do with lists, or how can i make the other approach to work.

The relevant code snippet from xml layout that holds news contents is:

<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>

and code that corresponds to one comment is:

<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>

Thank you very much for any reply. This is quite important for me.


回答1:


I am not very sure how you inflate your comments, but here is how I would do it.

Declare the comments Layout as a LinearLayout and give it an id, and then get a reference to that LinearLayout :

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

and then just add child Views to this layout :

commentsLayout.addView(newComment);


来源:https://stackoverflow.com/questions/9908883/programmatically-add-views-inside-a-linearlayout-that-is-inside-a-scrollview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!