Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 835
Jeeten Parmar Avatar asked Jan 22 '26 10:01

Jeeten Parmar


1 Answers

ok i share my project code here may be it helps you..

This class calculate the height and automatically adjust the layoutparams.

Solution:-

 package com.kview;

 import android.content.Context;
 import android.util.AttributeSet;
 import android.widget.GridView;
 import android.widget.ListView;

 public class FullLengthListView extends ListView {

boolean expanded = true;

public FullLengthListView(Context context, AttributeSet attrs,
        int defaultStyle) {
    super(context, attrs, defaultStyle);
}

public FullLengthListView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public boolean isExpanded() {
    return expanded;
}

public void setExpanded(boolean expanded) {
    this.expanded = expanded;
}

@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    // HACK! TAKE THAT ANDROID!
    if (isExpanded()) {
        // Calculate entire height by providing a very large height hint.
        // View.MEASURED_SIZE_MASK represents the largest height possible.
        int expandSpec = MeasureSpec.makeMeasureSpec(MEASURED_SIZE_MASK,
                MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);

        android.view.ViewGroup.LayoutParams params = getLayoutParams();
        params.height = getMeasuredHeight();
    } else {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}
  }
like image 51
Kailash Dabhi Avatar answered Jan 24 '26 01:01

Kailash Dabhi