Paulo Rodrigues
Paulo Rodrigues

Reputation: 5303

LinearLayout scrollable with multiple ListView

This is my layout:

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dip"
        android:textSize="15sp"
        android:textStyle="bold"
        android:textColor="@android:color/holo_blue_dark"
        android:text="@string/section_1" />

    <ListView
        android:id="@+id/lv_section_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:drawSelectorOnTop="false" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dip"
        android:textSize="15sp"
        android:textStyle="bold"
        android:textColor="@android:color/holo_blue_dark"
        android:text="@string/section_2" />

    <ListView
        android:id="@+id/lv_section_2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:drawSelectorOnTop="false" />

</LinearLayout>

The problem is that when first section has many items and it takes up the whole screen, I can not scroll to see second section. With a quick search I just discovered that I can not use ListView within ScollView.

Is there any way to leave this LinearLayout scrollable so I can see all sections that can be added? I need something similar to the iOS UITableView, several sections and headers.

Thank you in advance.

Upvotes: 0

Views: 1437

Answers (2)

Paulo Rodrigues
Paulo Rodrigues

Reputation: 5303

Ok, just to have a list with multiple sections, what I could do to solve my problem was quite simple:

I left just one ListView and created a class CustomAdapter. And added items with different types:

ArrayList<HashMap<String, String>> listItems = new ArrayList<HashMap<String, String>>();

HashMap map = new HashMap<String, String>();
map.put("type", "section");
map.put("title", "Section 1");
listItems.add(map);

map = new HashMap<String, String>();
map.put("type", "item");
map.put("title", "Item A");
map.put("detail", "Detail A");
listItems.add(map);

Set adapter to my ListView:

CustomAdapter adapter = new CustomAdapter(context, R.layout.result_list, listItems);
ListView lv = (ListView) view.findViewById(R.id.resultlist);
lv.setAdapter(adapter);

In my CustomAdapter I set a different style for each type, section or item. Note that I just want something to differentiate items in a ListView.

Still accept suggestions for this problem if my solution is too ugly :)

Upvotes: 1

TNR
TNR

Reputation: 5869

Use layout_weight="1" and layout_height="0dp" for both ListViews which will split your entire screen into two parts, so that you can see both the sections. If you have to show TextViews as Headers on top of listviews, then take two linear layouts with above said weights and inside Linear Layout, place textview and listview with Vertical orientation.

Upvotes: 0

Related Questions