Android如何在一个活动中一个接一个地显示2个列表视图

我已经使用这个代码来显示两个列表视图。

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

问题是,这导致2个列表视图占据屏幕的一半。 我正在添加一个头像这样的列表。

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

我想在第一个列表结束之后出现第二个列表的标题。 我如何做到这一点?我如何使列表视图出现,以便第一个开始时,第一个结束? 谢谢

使用像这样:

删除线性布局。 使用相对布局,并在那里放置你的两个列表视图像这样。

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

滚动之前

滚动后 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(); } } } 

你应该使用ExpandableListViewhttp://developer.android.com/reference/android/widget/ExpandableListView.html )。 您将有两个部分,而不是两个列表视图,但他们(包括标题)将按照您所描述的alignment。

在一个容器中使用多个ListView并不是一个好习惯。 它会导致测量和性能问题。 更好的办法是使用单个ListView和几个视图types 。 在你的情况下,视图types可能是: listview1_viewtypelistview2_viewtypelistview2_header_viewtype 。 对于第一个ListView的标题,你可以使用标题。

当提供重量时,应该提供高度为0dp或wrap_content。 但是你没有这样做。 改变你的XML文件如下。 我希望它适合你。

根据您的意见,我正在编辑我的post根据需求的解决scheme

1-:创build一个只有2行的列表视图

1.1-:在第一个listview#1中添加一个列表视图作为一个行子

1.2-:在第一个listview#1中添加另一个列表视图作为第二行子

我希望通过这种方式可以取得成功。

我是新来的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只能有一个直接的孩子,所以你将把这个完整的布局作为孩子。

将此(下面)布局命名为final_layout.xml

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

现在在fragment / activity中使用这个final_layout.xml,并使用在two_listview_layout.xml中命名的Id来访问任何的listview。

输出是这样的:(道歉的质量差的屏幕录像机:p)预告片和评论 – 都是列表视图在这里。

在这里输入图像描述

尝试使用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)); } 

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

尝试使用相对布局:

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