Android的自动合并两个列表视图在一个屏幕视图、屏幕、两个、列表

2023-09-13 02:10:01 作者:爱到最美是陪伴

我的看法是这样的:

<?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>

我用了两个自定义适配器以显示两个列表视图(ListView的数据源完全不同的对方):

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-->

但问题是两个列表视图自动合并成一个的ListView ,和标题的TextView 消失(如果有一个的ListView ,头会显示)。 我不知道这个问题。你能告诉我在哪里,我错了吗?

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?

推荐答案

试试下面的例子: 布局:

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>

在你的类

 ListViewAdapter arrayAdapter = new ListViewAdapter(this, values);

            listview.setAdapter(arrayAdapter);

        ListViewAdapter arrayAdapter2 = new ListViewAdapter(this, values2);

            listview2.setAdapter(arrayAdapter2);

添加在的onCreate这两行与您的ListView:

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摘自:listViewNotScrolling

这肯定会努力! :)

 
精彩推荐
图片推荐