user3550820
user3550820

Reputation:

How to implement a ListView completely inside a ScrollView

Here, I'm using a listView inside a ScrollView, everything is working fine but listView isn't displaying completely when I set its heightfill_parent. So I've fixed its height and it doesn't look good when I change the android phone device. Can anyone help me in it? I'm posting my xml class too.

Demo.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">
<LinearLayout 
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="25dp"
        android:gravity="center"
        android:orientation="vertical" >

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:paddingLeft="6dp"
            android:text="@string/course_tracker"
            android:textStyle="bold" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/ll_finishapp_table"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="10dp"
        android:orientation="vertical" >

        <View
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:background="#E3E3E3" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#ffffff"
            android:orientation="horizontal"
            android:weightSum="20" >

            <TextView
                android:layout_width="2dp"
                android:layout_height="match_parent"
                android:background="#E3E3E3"
                android:text="" />

            <LinearLayout
                android:layout_width="0dip"
                android:layout_height="match_parent"
                android:layout_weight="5"
                android:orientation="vertical" >

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center_horizontal|center_vertical"
                    android:text="@string/program_name" />
            </LinearLayout>

            <TextView
                android:layout_width="2dp"
                android:layout_height="match_parent"
                android:background="#E3E3E3"
                android:text="" />

            <LinearLayout
                android:layout_width="0dip"
                android:layout_height="match_parent"
                android:layout_weight="2"
                android:gravity="center"
                android:orientation="vertical"
                android:background="#e9e9e9" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:text="@string/colon"
                    android:textStyle="bold" />
            </LinearLayout>

            <TextView
                android:layout_width="2dp"
                android:layout_height="match_parent"
                android:background="#E3E3E3"
                android:text="" />

            <LinearLayout
                android:layout_width="0dip"
                android:layout_height="match_parent"
                android:layout_weight="13"
                android:gravity="center_vertical"
                android:orientation="vertical" >

                <TextView
                    android:id="@id/tv_program_name"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:paddingLeft="10dp"
                    android:textStyle="bold" />
            </LinearLayout>

            <TextView
                android:layout_width="2dp"
                android:layout_height="match_parent"
                android:background="#E3E3E3"
                android:text="" />
        </LinearLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:background="#E3E3E3" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#F9F9F9"
            android:orientation="horizontal"
            android:weightSum="20" >

            <TextView
                android:layout_width="2dp"
                android:layout_height="match_parent"
                android:background="#E3E3E3"
                android:text="" />

            <LinearLayout
                android:layout_width="0dip"
                android:layout_height="match_parent"
                android:layout_weight="5"
                android:orientation="vertical" >

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center_horizontal|center_vertical"
                    android:text="@string/program_description" />
            </LinearLayout>

            <TextView
                android:layout_width="2dp"
                android:layout_height="match_parent"
                android:background="#E3E3E3"
                android:text="" />

            <LinearLayout
                android:layout_width="0dip"
                android:layout_height="match_parent"
                android:layout_weight="2"
                android:gravity="center"
                android:orientation="vertical"
                android:background="#e9e9e9" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:text="@string/colon"
                    android:textStyle="bold" />
            </LinearLayout>

            <TextView
                android:layout_width="2dp"
                android:layout_height="match_parent"
                android:background="#E3E3E3"
                android:text="" />

            <LinearLayout
                android:layout_width="0dip"
                android:layout_height="match_parent"
                android:layout_weight="13"
                android:gravity="center_vertical"
                android:orientation="vertical" >

                <TextView
                    android:id="@id/tv_program_description"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:paddingLeft="10dp"
                    android:textStyle="bold" />
            </LinearLayout>

            <TextView
                android:layout_width="2dp"
                android:layout_height="match_parent"
                android:background="#E3E3E3"
                android:text="" />
        </LinearLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:background="#E3E3E3" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="20dp"
        android:orientation="vertical" >

        <View
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:background="#E3E3E3" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:background="#ffffff"
            android:orientation="horizontal"
            android:weightSum="20" >

            <TextView
                android:layout_width="2dp"
                android:layout_height="match_parent"
                android:background="#E3E3E3"
                android:text="" />

            <LinearLayout
                android:layout_width="0dip"
                android:layout_height="match_parent"
                android:layout_weight="10"
                android:orientation="vertical" >

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center_horizontal|center_vertical"
                    android:text="@string/course_name" />
            </LinearLayout>

            <TextView
                android:layout_width="2dp"
                android:layout_height="match_parent"
                android:background="#E3E3E3"
                android:text="" />

            <LinearLayout
                android:layout_width="0dip"
                android:layout_height="match_parent"
                android:layout_weight="10"
                android:orientation="vertical" >

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center_horizontal|center_vertical"
                    android:text="@string/semester" />
            </LinearLayout>

            <TextView
                android:layout_width="2dp"
                android:layout_height="match_parent"
                android:background="#E3E3E3"
                android:text="" />
        </LinearLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:background="#E3E3E3" />

        <ListView
            android:id="@id/list_course"
            android:layout_width="match_parent"
            android:layout_height="350dp"
            android:layout_marginBottom="10dp" >
        </ListView>
    </LinearLayout>

 </LinearLayout></ScrollView>

Thanks.

Upvotes: 4

Views: 147

Answers (2)

Christoph Eberhardt
Christoph Eberhardt

Reputation: 450

Just use the headerView of the ListView: Put all the things you want above the list in the headerView and all the things you want below in the footerView. The listView will be shown at full height and the headerView is scrollable.

http://developer.android.com/reference/android/widget/ListView.html#addHeaderView(android.view.View,%20java.lang.Object,%20boolean)

http://developer.android.com/reference/android/widget/ListView.html#addFooterView(android.view.View,%20java.lang.Object,%20boolean)

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment, container, false);

    mListViewHeaderView = initHeaderView(mListView, inflater);

    mListView = (ListView) view.findViewById(R.id.listView);
    mListView.addHeaderView(mListViewHeaderView, null, false);

    return view;
}


private View initHeaderView(ViewGroup container, LayoutInflater inflater) {
    View headerView = inflater.inflate(R.layout.fragment_atm_details_header, container, false);

    // configure header view

    return headerView;
}

Upvotes: 0

user543
user543

Reputation: 3633

Dynamically set the height to ListView based on the list count,Like:

private void setListViewHeightBasedOnChildren(ListView listView) {
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null) {
        return;
    }

    int totalHeight = 0;
    for (int i = 0; i < listAdapter.getCount(); i++) {
        View listItem = listAdapter.getView(i, null, listView);
        if (listItem != null) {
            listItem.measure(
                    MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
                    MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
        }

        totalHeight += listItem.getMeasuredHeight();
    }

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

call above method after setting the adapter to the ListView,Like:

urListView.setAdapter(urAdapter);
setListViewHeightBasedOnChildren(urListView);

Upvotes: 1

Related Questions