Jeeten Parmar Jeeten Parmar - 4 months ago 57
Android Question

Listview Height issue inside ScrollView in Android

I am using

ListView
inside
ScrollView
in Android. So, as we all know, It will occur height issue. For that, I am setting
ListView
size using below code :

public static void getListViewSize(ListView myListView, Context context) {
ListAdapter myListAdapter = myListView.getAdapter();

if (myListAdapter == null) {
return;
}

int totalHeight = 0;
for (int size = 0; size < myListAdapter.getCount(); size++) {

View listItem = myListAdapter.getView(size, null, myListView);
if (listItem instanceof ViewGroup)
listItem.setLayoutParams(new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT));

WindowManager wm = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
@SuppressWarnings("deprecation")
int screenWidth = display.getWidth();
int listViewWidth = screenWidth - 65;
int widthSpec = MeasureSpec.makeMeasureSpec(listViewWidth,
MeasureSpec.AT_MOST);
listItem.measure(widthSpec, 0);

totalHeight += listItem.getMeasuredHeight();
}

ViewGroup.LayoutParams params = myListView.getLayoutParams();
params.height = totalHeight
+ (myListView.getDividerHeight() * (myListAdapter.getCount() - 1));
myListView.setLayoutParams(params);
myListView.requestLayout();
}


Xml :

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/white" >

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<ListView
android:id="@+id/lvHomeStream"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:divider="#b5b5b5"
android:dividerHeight="1dp" >
</ListView>

<TextView
android:id="@+id/tvMoreRecords"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:layout_marginTop="10dp"
android:background="@drawable/list"
android:gravity="center"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:text="@string/viewmore"
android:textColor="@color/blue"
android:textSize="16sp" />
</LinearLayout>

</ScrollView>


It works fine if height of each ListView item is same. But, if height of each
ListView
item is unusual, then it leave extra big space between
ListView
end boundary and
TextView
.


Please help me to solve this issue.

SMR SMR
Answer

As Dev Carlsberg suggested DONT USE ListView inside ScrollView you can change the xml to this:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/background_light" >

    <TextView
        android:id="@+id/tvMoreRecords"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="15dp"
        android:layout_marginTop="10dp"
        android:background="@drawable/list"
        android:gravity="center"
        android:paddingBottom="5dp"
        android:paddingTop="5dp"
        android:text="@string/viewmore"
        android:textColor="@color/blue"
        android:textSize="16sp" />

    <ListView
        android:id="@+id/lvHomeStream"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:divider="#b5b5b5"
        android:dividerHeight="1dp" >
    </ListView>

</RelativeLayout>

or alternatively you may find the

Android ListView with Load More Button

Android - ListView to load more items when reached end

Hope it helps :)