The problem is that I need to make sure that inside the ScrollView a ListView , and it scrolls together ( ListView ) ScrollView . That's how I implemented it, but between the ListView and the third container (footer) a large space appeared.

enter image description here

main_layout.xml

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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" xmlns:app="http://schemas.android.com/apk/res-auto" tools:context=".Details.Book.BookDetails" android:background="@color/fragment_bg"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary"/> <View android:layout_width="match_parent" android:layout_height="3dp" android:background="@drawable/toolbar_shadow" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_below="@+id/toolbar" android:id="@+id/view5" /> <ScrollView android:layout_width="wrap_content" android:layout_height="match_parent" android:id="@+id/scrollView6" android:layout_below="@+id/toolbar"> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@+id/toolbar"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:layout_below="@+id/toolbar" android:id="@+id/linearLayout16"> <RelativeLayout android:id="@+id/relativeLayout9" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:background="@drawable/rounded_top_layout" android:paddingBottom="@dimen/leftPadding" android:paddingLeft="@dimen/rightPadding" android:paddingRight="@dimen/rightPadding" android:paddingTop="@dimen/leftPadding"> <TextView android:id="@+id/textviewTitle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_alignParentTop="false" android:layout_centerVertical="true" android:text="@string/buy_det_pass_txt" android:textColor="@color/background_color" android:textSize="@dimen/mainLargeSize" /> </RelativeLayout> <RelativeLayout android:id="@+id/relativeLayout10" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/relativeLayout9" android:paddingBottom="@dimen/leftPadding" android:paddingLeft="@dimen/rightPadding" android:paddingRight="@dimen/rightPadding" android:paddingTop="@dimen/leftPadding"> <LinearLayout android:id="@+id/linearLayout4" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_below="@+id/relativeLayout9" android:layout_marginBottom="@dimen/textMargin" android:gravity="center_vertical" android:orientation="horizontal"> <TextView android:id="@+id/textView43" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/buy_det_fullname" android:textColor="@color/mainGreyColor" android:textSize="@dimen/mainSmallSize" /> <TextView android:id="@+id/tvFullName" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="right" android:text="Medium Text" android:textColor="@color/mainGreyColor" android:textSize="@dimen/mainMiddleSize" /> </LinearLayout> <LinearLayout android:id="@+id/linearLayout3" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_below="@+id/linearLayout4" android:layout_marginBottom="@dimen/textMargin" android:gravity="center_vertical" android:orientation="horizontal"> <TextView android:id="@+id/textView35" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/buy_det_gender" android:textColor="@color/mainGreyColor" android:textSize="@dimen/mainSmallSize" /> <TextView android:id="@+id/tvGender" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="right" android:text="Medium Text" android:textColor="@color/mainGreyColor" android:textSize="@dimen/mainMiddleSize" /> </LinearLayout> <LinearLayout android:id="@+id/linearLayout6" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_below="@+id/linearLayout3" android:layout_marginBottom="@dimen/textMargin" android:gravity="center_vertical" android:orientation="horizontal"> <TextView android:id="@+id/textView37" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/buy_det_ticknumber" android:textColor="@color/mainGreyColor" android:textSize="@dimen/mainSmallSize" /> <TextView android:id="@+id/tvTickNumber" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="right" android:text="Medium Text" android:textColor="@color/mainGreyColor" android:textSize="@dimen/mainMiddleSize" /> </LinearLayout> <LinearLayout android:id="@+id/linearLayout7" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_below="@+id/linearLayout6" android:layout_marginBottom="@dimen/textMargin" android:gravity="center_vertical" android:orientation="horizontal"> <TextView android:id="@+id/textView39" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/buy_det_issdate" android:textColor="@color/mainGreyColor" android:textSize="@dimen/mainSmallSize" /> <TextView android:id="@+id/tvIssDate" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="right" android:text="Medium Text" android:textColor="@color/mainGreyColor" android:textSize="@dimen/mainMiddleSize" /> </LinearLayout> </RelativeLayout> </LinearLayout> <ListView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/listView" android:layout_centerHorizontal="true" android:paddingRight="@dimen/textMargin" android:paddingLeft="@dimen/textMargin" android:layout_below="@+id/linearLayout16" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:layout_below="@+id/listView"> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:background="@drawable/rounded_top_layout" android:id="@+id/relativeLayout9" android:paddingTop="@dimen/leftPadding" android:paddingBottom="@dimen/leftPadding" android:paddingLeft="@dimen/rightPadding" android:paddingRight="@dimen/rightPadding"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/buy_det_cost_txt" android:id="@+id/tvFromTo" android:textColor="@color/background_color" android:textSize="@dimen/mainLargeSize" android:layout_alignParentTop="false" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_centerVertical="true" /> </RelativeLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/relativeLayout9" android:id="@+id/relativeLayout10" android:paddingRight="@dimen/rightPadding" android:paddingLeft="@dimen/rightPadding" android:paddingTop="@dimen/leftPadding" android:paddingBottom="@dimen/leftPadding"> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/relativeLayout9" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:id="@+id/linearLayout4" android:gravity="center_vertical" android:layout_marginBottom="@dimen/textMargin" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/buy_det_fare" android:id="@+id/textView43" android:layout_weight="1" android:textColor="@color/mainGreyColor" android:textSize="@dimen/mainSmallSize" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Medium Text" android:id="@+id/tvFare" android:layout_weight="1" android:textColor="@color/mainGreyColor" android:gravity="right" android:textSize="@dimen/mainMiddleSize" /> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/linearLayout4" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:id="@+id/linearLayout3" android:gravity="center_vertical" android:layout_marginBottom="@dimen/textMargin"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/buy_det_taxes" android:id="@+id/textView35" android:layout_weight="1" android:textColor="@color/mainGreyColor" android:textSize="@dimen/mainSmallSize" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Medium Text" android:id="@+id/tvTaxes" android:layout_weight="1" android:textColor="@color/mainGreyColor" android:gravity="right" android:textSize="@dimen/mainMiddleSize" /> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/linearLayout3" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:id="@+id/linearLayout6" android:gravity="center_vertical" android:layout_marginBottom="@dimen/textMargin"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/buy_det_service_fees" android:id="@+id/textView37" android:layout_weight="1" android:textColor="@color/mainGreyColor" android:textSize="@dimen/mainSmallSize" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Medium Text" android:id="@+id/tvFees" android:layout_weight="1" android:textColor="@color/mainGreyColor" android:gravity="right" android:textSize="@dimen/mainMiddleSize" /> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/linearLayout6" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:id="@+id/linearLayout7" android:gravity="center_vertical" android:layout_marginBottom="@dimen/textMargin"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/buy_det_total_price" android:id="@+id/textView39" android:layout_weight="1" android:textColor="@color/mainGreyColor" android:textSize="@dimen/mainSmallSize" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Medium Text" android:id="@+id/tvTotPrice" android:layout_weight="1" android:textColor="@color/mainGreyColor" android:gravity="right" android:textSize="@dimen/mainMiddleSize" /> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/linearLayout7" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:id="@+id/linearLayout15" android:gravity="center_vertical" android:layout_marginBottom="@dimen/textMargin" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/buy_det_form_of_pay" android:id="@+id/textView54" android:layout_weight="1" android:textColor="@color/mainGreyColor" android:textSize="@dimen/mainSmallSize" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Medium Text" android:id="@+id/tvPayment" android:layout_weight="1" android:textColor="@color/mainGreyColor" android:gravity="right" android:textSize="@dimen/mainMiddleSize" /> </LinearLayout> </RelativeLayout> </LinearLayout> </LinearLayout> </ScrollView> </RelativeLayout> 

MainActivity.java

 ListView listView = (ListView)findViewById(R.id.listView); ListUtils.setDynamicHeight(listView); 

ListUtils class

 public static class ListUtils { public static void setDynamicHeight(ListView mListView) { ListAdapter mListAdapter = mListView.getAdapter(); if (mListAdapter == null) { return; } int height = 0; int desiredWidth = View.MeasureSpec.makeMeasureSpec(mListView.getWidth(), View.MeasureSpec.UNSPECIFIED); for (int i = 0; i < mListAdapter.getCount(); i++) { View listItem = mListAdapter.getView(i, null, mListView); listItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED); height += listItem.getMeasuredHeight(); } ViewGroup.LayoutParams params = mListView.getLayoutParams(); params.height = height + (mListView.getDividerHeight() * (mListAdapter.getCount() - 1)); mListView.setLayoutParams(params); mListView.requestLayout(); } } 

Please tell me how to fix?

  • one
    You shouldn’t even try to make such a city - think how else can you do what you want without wrapping the ListView in ScrollView - Juriy Spb
  • one
    Tell me, do you yourself as you understand all these textView39 and relativeLayout10 ? To understand what is for what and why absolutely impossible. - pavlofff
  • pavlofff, I do not use them, so the IDs are not renamed. - DevOma
  • YuriySPb, did not quite understand - DevOma
  • one
    Here you have the main problem that if there are more items in the central list than enters the screen, nothing will scroll out of the conflict between ScrollView and ListView / Since you have a small number of items, I recommend programmatically filling the required number of points with routes to normal container, not ListView - pavlofff

3 answers 3

Remove the ScrollView and leave only the ListView , but add a Header and Footer to it - addHeaderView (), addFooterView (). This must be done before setAdapter() .

See example

  • I did this, but I cannot insert the general background central container. - DevOma
  • In the sense of? Which container? Gray background or frame? - anber
  • Container route! - DevOma

Try to take the usual LinearLayout. In it to attach TehtView "Passenger", ListView "Route". Set the ListView attribute android: layout_weight = "1" and TechTView "Total". LinearLayout set the required background.

  • Without ScrollView ??? - DevOma
  • yes, no scrollview. when the need to scroll appears on the screen, the listview itself will do it - ZigZag

Can someone help! Instead, ListView used NonScrollListView .

 public class NonScrollListView extends ListView { public NonScrollListView(Context context) { super(context); } public NonScrollListView(Context context, AttributeSet attrs) { super(context, attrs); } public NonScrollListView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int heightMeasureSpec_custom = MeasureSpec.makeMeasureSpec( Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, heightMeasureSpec_custom); ViewGroup.LayoutParams params = getLayoutParams(); params.height = getMeasuredHeight(); } }