Android 在一个屏幕中自动合并两个列表视图 [英] Android auto merge two listview in one screen
本文介绍了Android 在一个屏幕中自动合并两个列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的观点如下:
<?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);
我期望的是:
<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 中添加这两行:
Add these two lines in onCreate with your listview:
Utility.setListViewHeightBasedOnChildren(listview);
Utility.setListViewHeightBasedOnChildren(listview2);
在您的 Activity 中创建一个内部类:
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);
}
}
代码取自:listViewNotScrolling
它肯定会起作用!:)
这篇关于Android 在一个屏幕中自动合并两个列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文