Programmatically add views inside a (linear) layout (which is inside a ScrollView)

I have an application that after some clicking shows activity with news content. I want the bottom of this to display comments that are dynamically loaded into the async task.

One approach is to use a ListView and a custom ArrayAdapter, but I will have to put the ListView inside a ScrollView, and this is a problem even if I manually override the height of the ListView. It shows one element of the list and one part of the next.

Another approach is to define LinearLayout as the owner for comments than to inflate another LinearLayouts defined in it with its own XML file, fill its contents, attach to the holder view. It works fine from a software point of view, except that it pushes comments below the content of the news. It looks like it creates another scrollable kind of comment under the content of the news (the content overlaps it).

Is there any other way to do this that has nothing to do with lists, or how I can get a different approach to work.

Corresponding code snippet from xml layout that contains news content:

<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 matches one comment:

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

Thanks for any answer. It is very important for me.

+4
source share
1 answer

I'm not very sure how you are pouting your comments , but here's how I do it.

Declare Layout comments as LinearLayout and give it an identifier, and then get a link to LinearLayout :

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

and then just add the Views child to this layout:

 commentsLayout.addView(newComment); 
+2
source

Source: https://habr.com/ru/post/1403912/


All Articles