2

I am unable to display two sequential full-sized (in height) ListViews that would not be scrollable individually but only by scrolling the root element.

This thing is haunting me all day long since I had various outputs (two scrollable list views, half-cut scrollview, etc) but none succeeded. Read previous posts here, here and here.

Providing you the current layout xml, what could be the solution? Removal of ScrollView, changing LinearLayout to RelativeLayout, setting layout_weight?

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

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

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            >

            <xxx.xxx.xxx.xxx.MultiSpinner
                android:id="@+id/ad_list_filter"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.5"
                />

            <xxx.xxx.xxx.xxx.MultiSpinner
                android:id="@+id/ad_list_sort"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.5"
                />

        </LinearLayout>

        <ListView
            android:id="@+id/adListInterests"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_marginLeft="3dp"
            android:layout_marginRight="3dp"
            android:divider="@null"
            android:dividerHeight="5dp"
            >
        </ListView>

        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:background="@drawable/ad_list_seperator"
            />

        <ListView
            android:id="@+id/adListNoninterests"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_marginLeft="3dp"
            android:layout_marginRight="3dp"
            android:divider="@null"
            android:dividerHeight="5dp"
            >
        </ListView>
    </LinearLayout>
</ScrollView>

Thanks in advance!

Edit:

After removing the suggested ScrollView results in two scrollable lists. According to comments, I will try to achieve it by using the provided plugins, however I still hope for finding solution based on modifying layout code only. Thank you for your time!

Image:

Preview

Layout:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        >

        <xxx.xxx.xxx.xxx.MultiSpinner
            android:id="@+id/ad_list_filter"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.5"
            />

        <xxx.xxx.xxx.xxx.MultiSpinner
            android:id="@+id/ad_list_sort"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.5"
            />

    </LinearLayout>
    <ListView
        android:id="@+id/adListInterests"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_marginLeft="3dp"
        android:layout_marginRight="3dp"
        android:divider="@null"
        android:dividerHeight="5dp"
        >
    </ListView>

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="@drawable/ad_list_seperator"
        />

    <ListView
        android:id="@+id/adListNoninterests"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_marginLeft="3dp"
        android:layout_marginRight="3dp"
        android:divider="@null"
        android:dividerHeight="5dp"
        >
    </ListView>
</LinearLayout>
Community
  • 1
  • 1
sitilge
  • 3,687
  • 4
  • 30
  • 56
  • The `Removal of ScrollView` is **highly recommended** (it's really a **worse practice**). What does this `ListViews that would not be scrollable individually but only by scrolling the root element` mean? Maybe, you can rethink your UI to use an `ExpandableListView` (parent/children relation)? – Phantômaxx Jan 02 '15 at 17:27
  • @DerGolem just wanted to say that the lists should be fully shown and scrolling must happen on ScrollView. – sitilge Jan 03 '15 at 01:21
  • Are you aware that putting ListViews (or any other **scrollable**) inside a ScrollView (or any other **scrollable**) is a **BAD** idea? – Phantômaxx Jan 03 '15 at 10:10
  • Yes. That is so because of the 'confusion' of which View to use - `ScrollView` or `ListView` when the scroll event is trigerred. – sitilge Jan 04 '15 at 00:44
  • Yes, there's a "conflict" in scrolling. I also think there's a certain CPU overhead. – Phantômaxx Jan 04 '15 at 09:13

2 Answers2

1

Your hierarchy is right, just change your ListViews code like this:

android:layout_height="wrap_content" <!-- cahnge this line -->
android:layout_weight="xxx" <!-- remove this line -->

However this is a poor, inefficient and memory hogging solution. By doing this you are forcing all the views off screen to remain in memory. A better solution would be to use only one list with custom adapter handling multiple datasets/viewtypes and a header view containing the spinners.

The easiest and quickest and most correct solution for you right now would be using the cwac-merge library which allows you to load one ListView with data from mulitple ListAdapters. Once you understand how it works and use it, you can change layout like so:

list_header.xml

You'll have to set the header view programmatically before you assign the merge adapter to the list view. You can inflate this layout XML.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
  <xxx.xxx.xxx.xxx.MultiSpinner.../>
  <xxx.xxx.xxx.xxx.MultiSpinner.../>
</LinearLayout>

list_main.xml

This is the main layout of your activity or fragment. It will contain just the list, which can handle everything you need.

<?xml version="1.0" encoding="utf-8"?>
<ListView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/adListInterests"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="3dp"
    android:layout_marginRight="3dp"
    android:divider="@null"
    android:dividerHeight="5dp"/>

How to use cwac-merge

You'll need to import it in Gradle (for Android Studio)

repositories {
    maven {
        url "https://repo.commonsware.com.s3.amazonaws.com"
    }
}

dependencies {
    compile 'com.commonsware.cwac:merge:1.1.+'
}

or download a couple of .jars (for Eclipse)

This is how the code will look:

View header = LayoutInflater.from(context).inflate(R.layout.list_header, null, false);

// get references for your Spinners here...

myListView.addHeader(header, null, false);

// setup your two adapters as you are doing now

MergeAdapter adapter = new MergeAdapter();
adapter.addAdapter(firstAdapter); // the adapter that you previously used for the first list
adapter.addAdapter(secondAdapter); // the adapter that you previously used for the second list
myListView.setAdapter(adapter);
Eugen Pechanec
  • 37,669
  • 7
  • 103
  • 124
  • Thanks, but could I achieve by not using any library? Just refactoring the code? – sitilge Jan 03 '15 at 01:22
  • Why not use the library? It's distributed as a `.jar` sou you can easily import it to Android Studio and Eclipse. I updated the answer on how to use it, it's really chort piece of code. In any case you should copy the layout from my answer and if you're not using the library you'll have to implement custom `BaseAdapter`. – Eugen Pechanec Jan 03 '15 at 14:40
0

The whole idea about having two separate ListView was wrong - instead of that I built a single ListView and added a custom row as header for the following list after the first one has ended.

The concept I used is described here.

Community
  • 1
  • 1
sitilge
  • 3,687
  • 4
  • 30
  • 56