Distributing a ViewView Scroll List Inside a LinearLayout

I have a LinearLayout that has buttons and lists. Having received many buttons, I decided to put this LinearLayout in a ScrollView, but after installing ScrollView in ListViews, only one item is displayed.

Below is the code before ScrollView

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".WorkplaceObservationActivity" > <Button android:id="@+id/btn_reportType" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Report Type" /> <ListView android:id="@+id/lv_reportType" android:layout_width="match_parent" android:layout_height="wrap_content" android:visibility="gone" > </ListView> <Button android:id="@+id/btn_defineJob" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Define Job" /> <ListView android:id="@+id/lv_Jobs" android:layout_width="match_parent" android:layout_height="wrap_content" android:checkMark="?android:attr/listChoiceIndicatorMultiple" android:visibility="gone" > </ListView> <Button android:id="@+id/btn_timeLocation" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Time and Location" /> <LinearLayout android:id="@+id/ll_timeLocation" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:visibility="gone" > <Button android:id="@+id/btn_setCurrentTime" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right" android:text="Use current time and date" /> <Button android:id="@+id/btn_setCustomTime" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right" android:text="Enter custom time" /> <Button android:id="@+id/btn_setCustomDate" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right" android:text="Enter custom date" /> <TextView android:id="@+id/tv_time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="Time and Date: " android:textAppearance="?android:attr/textAppearanceMedium" android:visibility="gone" /> </LinearLayout> <Button android:id="@+id/btn_ReportDetails" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Report Details" /> <LinearLayout android:id="@+id/ll_ReportDetails" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:visibility="gone" > <Button android:id="@+id/btn_CaptureVideo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="Capture Video" /> <ListView android:id="@+id/lv_ReportDetailsItems" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" > </ListView> </LinearLayout> <Button android:id="@+id/btn_Review" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Review" /> <LinearLayout android:id="@+id/ll_Review" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <TextView android:id="@+id/tv_Review_ReportType" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Report Type: " android:textAppearance="?android:attr/textAppearanceMedium" /> <TextView android:id="@+id/tv_Review_DateTime" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Date and Time: " android:textAppearance="?android:attr/textAppearanceMedium" /> <TextView android:id="@+id/tv_Review_SupportingFiles" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Supporting Files: " android:textAppearance="?android:attr/textAppearanceMedium" /> <ListView android:id="@+id/lv_ReviewItems" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:visibility="gone" > </ListView> <Button android:id="@+id/btn_Review_Send" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Send" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:weightSum="2" > <Button android:id="@+id/btn_Review_Save" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="Save Draft" /> <Button android:id="@+id/btn_Review_Discard" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="Discard" /> </LinearLayout> </LinearLayout> </LinearLayout> 

for the above code I get the following screenshot enter image description here

Below is the xml code after ScrollView

 <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".WorkplaceObservationActivity" > <Button android:id="@+id/btn_reportType" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Report Type" /> <ListView android:id="@+id/lv_reportType" android:layout_width="match_parent" android:layout_height="wrap_content" android:visibility="gone" > </ListView> <Button android:id="@+id/btn_defineJob" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Define Job" /> <ListView android:id="@+id/lv_Jobs" android:layout_width="match_parent" android:layout_height="wrap_content" android:checkMark="?android:attr/listChoiceIndicatorMultiple" android:visibility="gone" > </ListView> <Button android:id="@+id/btn_timeLocation" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Time and Location" /> <LinearLayout android:id="@+id/ll_timeLocation" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:visibility="gone" > <Button android:id="@+id/btn_setCurrentTime" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right" android:text="Use current time and date" /> <Button android:id="@+id/btn_setCustomTime" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right" android:text="Enter custom time" /> <Button android:id="@+id/btn_setCustomDate" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right" android:text="Enter custom date" /> <TextView android:id="@+id/tv_time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="Time and Date: " android:textAppearance="?android:attr/textAppearanceMedium" android:visibility="gone" /> </LinearLayout> <Button android:id="@+id/btn_ReportDetails" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Report Details" /> <LinearLayout android:id="@+id/ll_ReportDetails" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:visibility="gone" > <Button android:id="@+id/btn_CaptureVideo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="Capture Video" /> <ListView android:id="@+id/lv_ReportDetailsItems" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" > </ListView> </LinearLayout> <Button android:id="@+id/btn_Review" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Review" /> <LinearLayout android:id="@+id/ll_Review" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <TextView android:id="@+id/tv_Review_ReportType" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Report Type: " android:textAppearance="?android:attr/textAppearanceMedium" /> <TextView android:id="@+id/tv_Review_DateTime" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Date and Time: " android:textAppearance="?android:attr/textAppearanceMedium" /> <TextView android:id="@+id/tv_Review_SupportingFiles" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Supporting Files: " android:textAppearance="?android:attr/textAppearanceMedium" /> <ListView android:id="@+id/lv_ReviewItems" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:visibility="gone" > </ListView> <Button android:id="@+id/btn_Review_Send" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Send" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:weightSum="2" > <Button android:id="@+id/btn_Review_Save" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="Save Draft" /> <Button android:id="@+id/btn_Review_Discard" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="Discard" /> </LinearLayout> </LinearLayout> </LinearLayout> </ScrollView> 

and the next sceen shot above

enter image description here

I want to show a complete list of items after scrollview.

+4
source share
5 answers

There is a conflict between ScrollView and ListView . In general, you cannot put a ListView inside a ScrollView . ListView already understands scrolling and will scroll itself. ListView needs to know how much vertical space on the screen it needs to use so that it can control the scrolling and display of its children. It's hard to understand how the layout you published should behave.

In any case, the only way to place a ListView inside a ScrollView is to tell ListView exactly how much vertical space it should use. You cannot use match_parent or wrap_content for layout_height , you need to fix the vertical size. This helps ScrollView determine how much vertical space each of its children occupies, and then ScrollView can do it right.

Try setting android:layout_height="80dp" to all your ListView to see how this happens.

EDIT: looking at the layout again, I have another suggestion

If you look at the layout again, I assume that you want to see only one of the ListView at the moment (the rest will be GONE ). In this case, you can try the following:

  • Remove the surrounding ScrollView
  • Set android:layout_weight="1" and android:layout_height="1px" on all ListView s

Basically, this tells the layout manager that the ListView should be provided with all the available space on the screen (i.e.: everything that remains after another View laid out). This will lead to the fact that the ListView extension will occupy all the free space on the screen, retaining all the other View on the screen, and the whole screen will not scroll, only the ListView . This may be the best way to give you what you want.

You might also consider using an ExpandableListView , which seems to be what you are trying to recreate here.

+10
source

You cannot directly or indirectly have a ListView inside a ScrollView . This is because they will be able to control touch events. You should think about the design of your interface.

+2
source

Do one thing, try giving a lowercase coded height for your list, for example android: layou_height = "100dp" and it will work, but this is not recommended.

+1
source

Try changing android: layout_width = "wrap_content" to android: layout_width = "match_parent" o listview.

0
source

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


All Articles