0

Hi i have try insert listview in scrollview but i have this problem: enter image description here

the space that scrollview reserve to listview is little, i want that scrollview is than the screen , i want scrollview is visible only when listview becomes larger than the screen How i do?

this is code:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <LinearLayout android:layout_width="match_parent"
        android:layout_height="match_parent"
        xmlns:tools="http://schemas.android.com/tools"
        android:orientation="vertical"
        android:id="@+id/activity_lista__eventi"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="14dp"
        android:paddingRight="14dp"
        android:paddingTop="@dimen/activity_vertical_margin"
        xmlns:android="http://schemas.android.com/apk/res/android"
        tools:context="com.example.fra87.eudroid.activity_class.Lista_Eventi">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:orientation="vertical">
            <SearchView
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:queryHint="Evento"
                android:id="@+id/cercaEvento"/>

        </LinearLayout>


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="10dp"
            android:id="@+id/linearLayoutEventi">

            <ListView
                android:id="@+id/listViewEventi"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/cercaEvento"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="15dp" />

        </LinearLayout>

    </LinearLayout>
</ScrollView>

How i do this?

Fra87
  • 297
  • 2
  • 5
  • 16

1 Answers1

0

More solutions can be found here: Android list view inside a scroll view

This is a common issue that a lot of developer face. The issue is you are stacking a scrollable view inside another scrollable view. One solution to remove the listview from the scrollview.

Another is the following code:

public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter(); 
if (listAdapter == null) {
    // pre-condition
    return;
}

int totalHeight = 0;
for (int i = 0; i < listAdapter.getCount(); i++) {
    View listItem = listAdapter.getView(i, null, listView);
    listItem.measure(0, 0);
    totalHeight += listItem.getMeasuredHeight();
}

ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();

}

Community
  • 1
  • 1
fsebek
  • 389
  • 2
  • 9