-4

This is my code, Its giving me only single ListView, If i set height of ListView to wrap_content or match_parent. But if I define a height in terms of dp, it starts showing me both ListView. But it doesn't look good in that case.

<ScrollView  xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

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

        <TextView
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:text="ListView1"
            android:gravity="center_vertical"
            android:paddingLeft="16dp"
            android:layout_marginTop="45dp"
            android:textColor="@color/white"
            android:textStyle="bold" />

        <ListView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/lv_upcoming"
            android:paddingLeft="4dp"
            android:paddingRight="4dp"
            android:divider="@null" />

        <View
            android:layout_width="fill_parent"
            android:layout_height="0.5dp"
            android:background="#ffffff" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:text="List Number 2"
            android:gravity="center_vertical"
            android:textColor="@color/white"
            android:fontFamily="sans-serif-condensed"
            android:textSize="18sp"
            android:id="@+id/tvPrevious"
            android:paddingLeft="16dp"
            android:paddingRight="16dp" />

       <ListView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/list_view_2"
            android:paddingLeft="4dp"
            android:paddingRight="4dp"
            android:divider="@null"/>

    </LinearLayout>
</ScrollView>

Kindly guide me how to add multiple ListView in a layout.

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
dev90
  • 7,187
  • 15
  • 80
  • 153

3 Answers3

1

Its always recommended to have a single ListView in a single layout file. But yes, there might appear some case where you need to fit two lists. So in that case, I would like to suggest merging both lists in a single ArrayList so that it can be shown in a single ListView.

Now there are other ways to show multiple ListView in a layout. Try using NestedScrollView instead of ScrollView.

If you consider using RecyclerView here's an implementation too of showing multiple lists in a same RecyclerView. You can find the project in the code section and the wiki is well documented too.

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
  • Thanks, I tried to use `NestedListView`, Its displaying both list items, but it reduced the size of List Item, and displaying only 1 item in the listView. I tried using `wrap_content` and `match_parent` both. If i define, let say `200dp`, it displays 2 items, but scroll View is not there any more in the List View – dev90 Oct 03 '16 at 12:17
  • Not `NestedListView` .. Its actually `ListView` inside `NestedScrollView`. Anyway, I would rather suggest you go for `RecyclerView` implementation. Its simple and easier. – Reaz Murshed Oct 03 '16 at 13:12
0

Put ListView in FrameLayout and set layout_weight to 1.

    <LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:text="ListView1"
            android:gravity="center_vertical"
            android:paddingLeft="16dp"
            android:layout_marginTop="45dp"
            android:textStyle="bold"
            android:textColor="@color/white"/>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">

        <ListView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/lv_upcoming"
                android:paddingLeft="4dp"
                android:paddingRight="4dp"
                android:divider="@null"
                />
    </FrameLayout>

    <View
            android:layout_width="fill_parent"
            android:layout_height="0.5dp"
            android:background="#ffffff" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:text="List Number 2"
            android:gravity="center_vertical"
            android:fontFamily="sans-serif-condensed"
            android:textSize="18sp"
            android:id="@+id/tvPrevious"
            android:paddingLeft="16dp"
            android:textColor="@color/white"
            android:paddingRight="16dp" />

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">

        <ListView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/list_view_2"
                android:paddingLeft="4dp"
                android:paddingRight="4dp"
                android:divider="@null"
            />
    </FrameLayout>

</LinearLayout>
0

Using list-view under scroll-view is not a good practice then also if you want to use the same. Then you have to keep in mind that list-view under scroll view does not work well and you have to manage the height of list-view pro-grammatically . you will get this issue when you set the adapter on both list-view . Resolution of the same is given below which i have seen on the below link:-

Android ListView rows in ScrollView not fully displayed - clipped

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);
    }
Community
  • 1
  • 1
kishna.147
  • 167
  • 8