0

I have started building an app with two ListView's under each other.

Now i put an ScrollView over it, because the amout of itmes might get very big and therefore i want the whole layout to be scrollable and not the lists itself. But my solution dosent work!

btw: i dont want to replace my ListViews with a LinearLayout or so because then all my adapters are gone.

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <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:orientation="vertical">

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

            <TextView
                style="@style/Heading"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="ListView1" />

            <Space
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1" />

            <Button
                style="@style/ButtonStyle.SpinnerAddButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:drawableRight="@drawable/ic_add_black_24dp"
                android:onClick="addToList1" />

        </LinearLayout>

        <ListView
            android:id="@+id/listView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/marginLeft"
            android:layout_marginRight="@dimen/marginLeft" />


    <!--Second list-->

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

            <TextView
                style="@style/Heading"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="ListView2" />

            <Space
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1" />

            <Button
                style="@style/ButtonStyle.SpinnerAddButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:drawableRight="@drawable/ic_add_black_24dp"
                android:onClick="addToList2" />

        </LinearLayout>

        <ListView
            android:id="@+id/listView2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/marginLeft"
            android:layout_marginRight="@dimen/marginLeft" />

    </LinearLayout>
</ScrollView>

My Layout now shows all the ListViews under each other but only one Item per ListView and you can then scroll through this Lists.

kk77
  • 1
  • 1
  • 1
    Putting vertically scrollable things in a `ScrollView` is difficult to get right. I would strongly encourage you to come up with a different UI, such as using two pages in a `ViewPager`, one for each list+button pair. – CommonsWare Jan 23 '15 at 19:06
  • 1
    `ScrollView` + `ListView` + `ListView` = bad things will happen. – Simas Jan 23 '15 at 19:06
  • A scrollable inside another scrollable is really a **worst design** UI. – Phantômaxx Jan 23 '15 at 19:08
  • thank you for your suggestion, i have to say not a bad idea, but maybe someone has a different option for me :-D – kk77 Jan 23 '15 at 19:09
  • Here is some workaround http://stackoverflow.com/questions/3495890/how-can-i-put-a-listview-into-a-scrollview-without-it-collapsing, but this is really bad design! – Miroslav Michalec Jan 23 '15 at 19:10
  • 1
    ok i think 3 opinions can convice me to get rid of my current UI – kk77 Jan 23 '15 at 19:12
  • As an **alternative design**, let me suggest you to use some `placeholders` (Buttons, TextViews, ImageViews... whatever) **in place** of the ListViews. By clicking one of these ones, you will show a **DialogFragment** featuring a ListView (imagine it as some kind of `floating ListView`). – Phantômaxx Jan 23 '15 at 19:15

1 Answers1

0

You should should use one listview and just have different viewtypes.

@Override
public int getItemViewType(int position) {
    return isFirstItem(position) ? TYPE_ITEMA : TYPE_ITEMB;
}

@Override
public int getViewTypeCount() {
    return 2;
}
Mark Hetherington
  • 1,612
  • 14
  • 24