2

My view look like:

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

 <!--list 1-->
<ListView
    android:id="@+id/lvMenu"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:cacheColorHint="@android:color/transparent"
    android:layout_marginBottom="10dp" />

 <!--the header-->
<LinearLayout
android:id="@+id/llIndustries"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/dark_grey_title_bar"
android:paddingBottom="5dp"
android:paddingLeft="10dp"
android:paddingTop="5dp" >
 <TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="@string/industry_clusters"
    android:textSize="16sp"/>
 </LinearLayout>

  <!--list 2-->
  <ListView
    android:id="@+id/lvIndustries"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:cacheColorHint="@android:color/transparent" />
 </LinearLayout>

I use two custom adapter to display two list view (the listview data source completely different each other):

private List<MenuInfo> menuData;
private List<MenuInfo> industriesData;

lvMenu = (ListView) layoutRoot.findViewById(R.id.lvMenu);
lvIndustries = (ListView) layoutRoot.findViewById(R.id.lvIndustries);

menuAdapter = new MenuAdapter(getActivity(), menuData);
lvMenu.setAdapter(menuAdapter); 

industriesAdapter = new MenuAdapter(getActivity(), industriesData);     
lvIndustries.setAdapter(industriesAdapter);

What I expect is:

 <ListView/> <!-- listview 1-->
 <TextView/> <!-- header-->
 <ListView/> <!-- listview 2-->

But the problem is two list view auto merged into one ListView, and the header TextView disappear (If has one ListView, the header will be showed).
I have no idea for this issue. Could you please tell me where I am wrong?

flx
  • 14,146
  • 11
  • 55
  • 70
ductran
  • 10,043
  • 19
  • 82
  • 165

2 Answers2

3

Try the below example: 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="match_parent"
    android:orientation="vertical"
    android:weightSum="3" >

    <ListView
        android:id="@+id/list2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@drawable/face"
        android:cacheColorHint="#00000000" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_weight="1"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Header"
            android:visibility="visible" />

        <ListView
            android:id="@+id/list"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/background"
            android:cacheColorHint="#00000000" />
    </LinearLayout>



</LinearLayout>

In your class

 ListViewAdapter arrayAdapter = new ListViewAdapter(this, values);

            listview.setAdapter(arrayAdapter);

        ListViewAdapter arrayAdapter2 = new ListViewAdapter(this, values2);

            listview2.setAdapter(arrayAdapter2);

Add these two lines in onCreate with your listview:

Utility.setListViewHeightBasedOnChildren(listview);
        Utility.setListViewHeightBasedOnChildren(listview2);

Create a inner class in your Activity:

//Test scrollview custom
    public static class Utility {
        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);
        }
    }

Code taken from : listViewNotScrolling

It would definitely work! :)

Community
  • 1
  • 1
Manishika
  • 5,478
  • 2
  • 22
  • 28
  • Thanks, it worked for me too. But the second listview always scroll (I don't want both listviews scrollable, instead to root layout should be scrollable). I tried to set `android:scrollbars="none"` for both listview and add scrollview for root layout but doesn't work. Could you recommend any idea? – ductran Oct 02 '13 at 05:58
  • You only want first listView to scroll and not second? – Manishika Oct 02 '13 at 07:05
  • I want both two list view not to scroll. The root layout contain 2 listview should scrollable. – ductran Oct 02 '13 at 07:23
  • You shouldn't use ListView inside ScrollView.What you can do is use simply LinearLayout inside ScrollView – Manishika Oct 02 '13 at 08:22
  • I dont know if these works but you can try them: 1. [scollLIst](http://stackoverflow.com/questions/3495890/how-can-i-put-a-listview-into-a-scrollview-without-it-collapsing/3495908#3495908) 2. [anotherLink](http://www.androidhub4you.com/2012/12/listview-into-scrollview-in-android.html) – Manishika Oct 02 '13 at 08:29
1

If you want to create Listview with Header. Then Use Section Header Listview.

Refer this Example.

Hope it will help you!!

OR

If its fix that Only Two Listview & one Header Between them

Then

1) Take Only one Listview.

2) Use Custom Adapter & Custom Layout for Listview Row.

3) Add Two Extra TextView in Custom Layout

Listing First ListView Data

  • (Fixed TextView - Header -Put its Visibility VISIBLE)
  • (Extra 1 TextView - Header -Put its Visibility GONE )
  • (Extra 2 TextView - Header -Put its Visibility GONE )

Listing Second ListView Data

  • (Fixed TextView - Header -Put its Visibility GONE )
  • (Extra 1 TextView - Header -Put its Visibility VISIBLE )
  • (Extra 2 TextView - Header -Put its Visibility VISIBLE )
Bhoomika Brahmbhatt
  • 7,404
  • 3
  • 29
  • 44