Android如何在一个Activity中依次显示两个ListView?

44

我已经使用这段代码将两个列表视图显示在彼此上方。

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

<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:background="#f00" >
</ListView>

<ListView
    android:id="@+id/listView2"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:background="#0f0" >
</ListView>

问题在于这将导致两个列表视图各自占据屏幕的一半。我正在为这两个列表都添加一个标题,方法如下。

LevelAdapter adapter = new LevelAdapter(getActivity(),
            R.layout.list_item, weather_data);

    View header = inflater.inflate(R.layout.header2, null);
    View header2 = inflater.inflate(R.layout.header, null);
   lv1.addHeaderView(header);
   lv2.addHeaderView(header2);
    lv1.setAdapter(adapter);
    lv2.setAdapter(adapter);

我希望第二个列表的标题出现在第一个列表结束后。我该怎么做?如何使列表视图显示,以便第二个列表从第一个列表结束时开始? 谢谢。

9个回答

107

Before scrolling

After scrolling activity_main.xml

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="10dip" >

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_vertical"
            android:text="ANDROID" />

        <ListView
            android:id="@+id/listView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dip"
            android:background="#B29090" >
        </ListView>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dip"
            android:gravity="center_vertical"
            android:text="IOS" />

        <ListView
            android:id="@+id/listView2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dip"
            android:background="#4A9C67" >
        </ListView>
    </LinearLayout>

</ScrollView>

MainActivity.java

package com.example.listviewin1xmldemo;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;

public class MainActivity extends ActionBarActivity {

    private ListView mListView1, mListView2;

    private String [] data1 ={"Hiren", "Pratik", "Dhruv", "Narendra", "Piyush", "Priyank"};
    private String [] data2 ={"Kirit", "Miral", "Bhushan", "Jiten", "Ajay", "Kamlesh"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mListView1 = (ListView)findViewById(R.id.listView1);
        mListView2 = (ListView)findViewById(R.id.listView2);

        mListView1.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data1));
        mListView2.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data2));

        ListUtils.setDynamicHeight(mListView1);
        ListUtils.setDynamicHeight(mListView2);
    }


    public static class ListUtils {
        public static void setDynamicHeight(ListView mListView) {
            ListAdapter mListAdapter = mListView.getAdapter();
            if (mListAdapter == null) {
                // when adapter is null
                return;
            }
            int height = 0;
            int desiredWidth = MeasureSpec.makeMeasureSpec(mListView.getWidth(), MeasureSpec.UNSPECIFIED);
            for (int i = 0; i < mListAdapter.getCount(); i++) {
                View listItem = mListAdapter.getView(i, null, mListView);
                listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
                height += listItem.getMeasuredHeight();
            }
            ViewGroup.LayoutParams params = mListView.getLayoutParams();
            params.height = height + (mListView.getDividerHeight() * (mListAdapter.getCount() - 1));
            mListView.setLayoutParams(params);
            mListView.requestLayout();
        }
    }
}

10
谢谢,伙计,那段代码非常好用......我之前试过这篇帖子中的其他代码,但只有部分有效。这应该是正确的顺序。像我这样的其他用户,请参考这个例子。这个百分百有效,其他的只是部分有效。 - Abhijit Gujar
1
@Hiren Patel,android.R.layout.simple_list_item_1在哪里?它是Android系统使用的通用布局ID吗?我收到了“您的内容必须具有id属性为'android.R.id.list'的Listview”的消息。 - user914425
1
@user914425,请问你能否复制完整的代码并运行它吗?希望它能对你有所帮助。 - Hiren Patel
1
终于在漫长的时间里得到了解决方案。我已经尝试了三天,这个答案对我很有用。感谢@HirenPatel发布这个答案。 - Shubhank Gupta
我可以用这个来实现可扩展的列表视图吗? - S HemaNandhini
显示剩余13条评论

46

使用方法如下:

移除线性布局。改用相对布局,并在其中放置两个列表视图,如下所示。

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

   <RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#f00" >
 </ListView>

 <ListView
android:id="@+id/listView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/listView1"
android:background="#0f0" >
</ListView>
    </RelativeLayout>
  </ScrollView>

添加 Utility.java

public class Utility {

    public static void setListViewHeightBasedOnChildren(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            // pre-condition
            return;
        }

        int totalHeight = 0;
        int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.AT_MOST);
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);
        listView.requestLayout();
    }
}

在您的活动中:

 lv1.setAdapter(adapter);
 lv2.setAdapter(adapter);

Utility.setListViewHeightBasedOnChildren(lv1);
Utility.setListViewHeightBasedOnChildren(lv2);

4
抱歉,这行不通。现在它们两个一起占据了屏幕的一半。你如何明确给出ListView的高度?另外,我使用了一个线性布局,在其中layout_below可能是无效的。 - Shivam Bhalla
2
抱歉,它无法正常工作,布局中只包含了一半的屏幕。 你有其他的想法吗? 请帮帮我... - patel135
它的运行很好,但如果我动态添加页眉或页脚,那么布局看起来就不合适了。 - Sachin C
6
不应该在 ScrollView 中放置 ListView。 - user3144836
这是一种非常糟糕的方法。因为在setListViewHeightBasedOnChildren()之后,列表视图将显示所有视图,而不是屏幕上的5-10个元素。如果您的适配器包含10k个项目,则应用程序会因OOM而崩溃。 - Pavel Shorokhov
显示剩余2条评论

9

如何在单个屏幕中使用多个可扩展列表视图? https://stackoverflow.com/q/49879295/6067866 - Vanraj Ghed

5

在单个容器中使用多个ListView不是一个好的做法。这会导致测量和性能问题。 更好的方法是使用单个ListView,其中包含几种视图类型。在您的情况下,视图类型可以是:listview1_viewtypelistview2_viewtypelistview2_header_viewtype。对于第一个ListView的标题,您可以只使用标题。


2

我对Android也很新,并且我有一个类似的解决方案(不完全是,但是是一种变通方法),通过创建一个布局文件,在其中放置所需数量的列表视图和另一个XML布局,将先前创建的布局包装在ScrollView中。

假设您有一个名为two_listview_layout.xml的布局定义:

<RelativeLayout
..>
    <ListView
        android:id="@+id/listViewOne"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <ListView
        android:id="@+id/listViewTwo"
        android:layout_width="match_parent"
        android:layout_width="wrap_content"/>
</RelativeLayout>

RelativeLayout在这里不是必须的,但您可以根据您的要求和布局进行调整。

现在创建另一个布局,将此布局包装在ScrollView中。由于ScrollView只能有1个直接子节点,因此您将使用此完整布局作为其子节点。

将此布局命名为final_layout.xml(如下所示)

<FrameLayout
...>
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <include layout="@layout/two_listview_layout" />
    </ScrollView>
</FrameLayout>

现在将final_layout.xml在您的fragment/activity中加载,并使用two_listview_layout.xml中命名的ID来访问任何listview。
输出结果如下所示:(抱歉录屏质量不佳 :p)Trailers和Reviews - 这里都是listviews。 enter image description here

2
当提供权重时,高度应该设为0dp或wrap_content。但是你没有这样做。请按照以下方式更改你的xml文件。希望对你有帮助。
根据你的评论,我根据要求编辑了我的帖子解决方案:
1-:创建一个只有2行的列表视图
1.1-:在第一个列表视图#1中添加一个列表视图作为行子项
1.2-:在第一个列表视图#1中添加另一个列表视图作为第二个行子项
我希望通过这种方式你可以成功实现。

你好。你能详细说明一下我如何在这里使用帧布局吗?可能有一些示例代码吗? - Shivam Bhalla

2

请尝试使用ScrollView和LinearLayout:

activity_main.xml

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
   <ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/action_bar1"
    android:fillViewport="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="10dip">

        <ListView
            android:id="@+id/listView1"
            android:layout_width="match_parent"
            android:layout_height="200dip"
            android:layout_margin="10dip"
            android:background="#B29090">
        </ListView>



        <ListView
            android:id="@+id/listview2"
            android:layout_width="match_parent"
            android:layout_height="200dip"
            android:layout_margin="10dip"
            android:background="#4A9C67">
        </ListView>
    </LinearLayout>
</ScrollView>
</RelativeLayout>

MainActivity.java

 private ListView list1, list2;
private String [] data1 ={"H", "P", "D", "N", "P", "P"};
private String [] data2 ={"K", "M", "B", "K", "A", "K"};
 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sounds);
    list1 = (ListView) findViewById(R.id.listView1);
    list2 = (ListView) findViewById(R.id.listview2);

    list1.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,android.R.id.text1,data1));
    list2.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,android.R.id.text1,data2));



}

0

在我的情况下,添加layout_weight起了作用。

<ListView
    android:id="@+id/excerciseListView"
    android:layout_width="match_parent"
    android:layout_height="0px"
    android:divider="#B6B6B6"
    android:dividerHeight="1sp"
    android:layout_weight="3"></ListView>
<ListView
    android:id="@+id/instructionListView"
    android:layout_width="match_parent"
    android:layout_height="0px"
    android:divider="#B6B6B6"
    android:dividerHeight="1sp" 
   android:layout_weight="1"></ListView>

-3

尝试使用相对布局:

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

<ListView
 android:id="@+id/listView1"
 android:layout_width="match_parent"
 android:layout_height="fill_parent"
 android:layout_weight="1"
 android:layout_below="@+id/listView1"
 android:background="#f00" >
</ListView>
<ListView
 android:id="@+id/listView2"
 android:layout_width="match_parent"
 android:layout_height="fill_parent"
 android:layout_weight="1"
 android:background="#0f0" >
</ListView>
</RelativeLayout>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接