Hello to all. Maybe someone faced the following problem or knows how to solve the following. After the activity opens, the listview is displayed immediately. activity is not displayed on top. If the height of the layout is placed on the screen, it shows as usual, as it should. But if the listview is filled with several elements, then the activity is automatically scrolled to the listview.

<include android:id="@+id/tool_bar" layout="@layout/toolbar" /> <ScrollView xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:fillViewport="true" android:background="@color/mainBackground" android:layout_below="@id/tool_bar"> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <RelativeLayout android:id="@+id/rlTopMaterialCard" android:layout_margin="5dp" android:background="@color/white" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView fontPath="fonts/Roboto-Regular.ttf" android:paddingTop="10dp" android:paddingLeft="10dp" android:paddingRight="10dp" android:id="@+id/tvProductionName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:textSize="24sp" android:textColor="@color/colorPrimary" android:text=""/> <TextView fontPath="fonts/Roboto-Regular.ttf" android:paddingLeft="10dp" android:paddingRight="10dp" android:ellipsize="end" android:id="@+id/tvCountry" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/tvProductionName" android:gravity="left" android:textColor="@color/symbol_black" android:textSize="15sp" android:text="" android:paddingTop="10dp" /> <TextView fontPath="fonts/Roboto-Regular.ttf" android:layout_below="@id/tvCountry" android:id="@+id/tvDescription" android:textColor="@color/gray" android:text="@string/descriptionDetails" android:layout_width="wrap_content" android:textSize="14sp" android:layout_height="wrap_content" android:paddingLeft="10dp" android:paddingRight="10dp" android:paddingBottom="10dp" android:paddingTop="10dp" /> </RelativeLayout> <com.daimajia.slider.library.SliderLayout android:layout_below="@+id/rlTopMaterialCard" android:id="@+id/sliderImages" android:layout_width="match_parent" custom:pager_animation="Default" custom:auto_cycle="true" custom:indicator_visibility="visible" custom:pager_animation_span="1100" android:layout_height="200dp" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:background="@color/gray"/> <com.daimajia.slider.library.Indicators.PagerIndicator android:layout_below="@+id/sliderImages" android:id="@+id/custom_indicator" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" custom:selected_color="#0095BF" custom:unselected_color="#55333333" custom:shape="oval" custom:selected_padding_left="5dp" custom:selected_padding_right="5dp" custom:unselected_padding_left="5dp" custom:unselected_padding_right="5dp" android:layout_centerHorizontal="true" custom:selected_width="6dp" custom:selected_height="6dp" custom:unselected_width="6dp" custom:unselected_height="6dp"/> <LinearLayout android:paddingTop="5dp" android:paddingBottom="5dp" android:layout_marginTop="5dp" android:layout_marginRight="5dp" android:layout_marginLeft="5dp" android:layout_below="@+id/custom_indicator" android:id="@+id/llShopText" android:orientation="horizontal" android:background="@color/white" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center"> <TextView fontPath="fonts/Roboto-Regular.ttf" android:id="@+id/tvShop" android:textColor="@color/black" android:text="@string/shops" android:layout_width="wrap_content" android:textSize="16sp" android:layout_height="wrap_content" /> </LinearLayout> <com.gc.materialdesign.views.ProgressBarCircularIndeterminate android:layout_below="@+id/llShopText" android:layout_centerInParent="true" android:id="@+id/pbLoadingShops" android:layout_width="50dp" android:layout_height="50dp" android:visibility="visible" android:background="@color/colorPrimary" /> <ListView android:layout_below="@+id/llShopText" android:layout_marginTop="0dp" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:layout_marginBottom="5dp" android:id="@+id/lvShops" android:layout_width="match_parent" android:layout_height="match_parent" android:drawSelectorOnTop="true"> </ListView> </RelativeLayout> </ScrollView> </RelativeLayout> 

The height of the listview is calculated as follows.

 if (shopsAdapter.getCount() > 0) { int desiredWidth = View.MeasureSpec.makeMeasureSpec(lvShops.getWidth(), View.MeasureSpec.AT_MOST); float totalHeight = 0; for (int i = 0; i < shopsAdapter.getCount(); i++) { View listItem = shopsAdapter.getView(i, null, lvShops); if (listItem instanceof ViewGroup) { listItem.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)); } listItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED); totalHeight += listItem.getMeasuredHeight(); } ViewGroup.LayoutParams params = lvShops.getLayoutParams(); params.height = (int) (totalHeight + (lvShops.getDividerHeight() * (lvShops.getCount() - 1))); lvShops.setLayoutParams(params); lvShops.requestLayout(); } 

    1 answer 1

    ScroolView is not designed to work with the ListView inside.

    In your case, the most elegant solution would be:

    1) Transferring all that is out of the ListView into its Header by the addHeader method (View v) .

    2) Since ScroolView will no longer be needed, it must be removed.

    So you will not need to be sophisticated with measuring the height of the ListView plus productivity, because Header is, technically, a ListView element.

    • And will Header scroll through? I have never come across this - gtr
    • Should not. This bug is most likely caused by the presence of a ListView in ScroolView. The method proposed in the answer is as if just adding another element to the ListView. - JuriySPb