Android: Scrollable preferences window with custom

2019-03-06 14:34发布

I have an activity with a listview that gets populated with preferences from an xml file, then another listview that gets populated by data in my database, then a textview and a edittext.

I want every element (including all the list views) on the screen to take their full height, and the entire screen be scrollable, but I cannot figure out how to do this!

Below is my xml:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scrollView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:fillViewport="true" >

    <LinearLayout
        android:id="@+id/llContainer"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <LinearLayout
            android:id="@+id/llPreferences"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <ListView
                android:id="@android:id/list"
                android:layout_width="fill_parent"
                    android:layout_height="wrap_content">
            </ListView>
        </LinearLayout>

        <LinearLayout
            android:id="@+id/llPlaces"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:weightSum="1" >

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="TextView"
                android:textAppearance="?android:attr/textAppearanceLarge" >
            </TextView>

            <ListView
                android:id="@+id/lstPlaces"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" >
            </ListView>

            <LinearLayout
                android:id="@+id/llAddNewPlace"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" >

                <EditText
                    android:id="@+id/txtNewPlaceName"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content" >
                </EditText>

                <Button
                    android:id="@+id/btnAddNewPlace"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Add New" >
                </Button>
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>

</ScrollView>

Any ideas why this is not performing as I want and would expect?

Thanks, Max.

3条回答
何必那么认真
2楼-- · 2019-03-06 14:45

Similer problem with solution on this thead:

Android ScrollView layout problem

Unfortunatly it looks like you cannot have a listview in a scrolllayout.

For people who are interested, looks like a potential workaround could be found here either now or in the future:

ListView in ScrollView potential workaround

查看更多
地球回转人心会变
3楼-- · 2019-03-06 14:46

Never put a ListView inside a ScrollView because there is no good way to differentiate between two nested scrollable containers.

查看更多
萌系小妹纸
4楼-- · 2019-03-06 15:09

Well, first off, there is really no reason to encapsulate your ListView inside of a Linear Layout inside of a Linear Layout. In fact, there is no reason to encapsulate any of your views inside two LinearLayouts. Try this and see what happens:

<?xml version="1.0" encoding="utf-8"?>

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scrollView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:fillViewport="true" >

    <LinearLayout
        android:id="@+id/llContainer"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <ListView
            android:id="@android:id/list"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">
        </ListView>

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView"
            android:textAppearance="?android:attr/textAppearanceLarge" >
        </TextView>

        <ListView
            android:id="@+id/lstPlaces"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >
        </ListView>

        <EditText
            android:id="@+id/txtNewPlaceName"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >
        </EditText>

        <Button
            android:id="@+id/btnAddNewPlace"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Add New" >
        </Button>

    </LinearLayout>

</ScrollView>
查看更多
登录 后发表回答