如何的Android后对方显示2列表视图在一个活动一 [英] Android how to display 2 listviews in one activity one after the other
问题描述
我已经使用这个code到另一个的上面显示2列表视图之一。
I have used this code to display 2 list view one on top of the other.
<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列表视图的每个占据屏幕的一半。 我加入一个头两个列表是这样的。
The problem is that, this causes the 2 listviews to each occupy half of the screen. I am adding a header to both lists like this.
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);
我想第二个列表的标题出现后的第一个列表结束。我该怎么办呢?如何让我的列表视图出现,使得第二个启动的时候,第一个是结束了吗? 谢谢
I would like the header of the second list to appear after the first list is over. How do i do this?How do i make the listviews appear such that the second one starts when the first one is over ? Thanks
推荐答案
使用这样的:
删除线布局。使用相对的布局和里面那个地方像这样的两个列表视图。
Remove Linear layout. use relative layout and inside that place your two list view like this.
<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);
这篇关于如何的Android后对方显示2列表视图在一个活动一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!