android中scrollview与listview共存
来源:互联网 发布:数学排列组合公式算法 编辑:程序博客网 时间:2024/06/05 03:32
来源:http://www.oschina.net/code/snippet_194968_7149
ScrollView与ListView共存会存在滚动的问题,并且ListView只显示一个半Item。 当ListView的高度设定一定的值时,ListView同样地会显示对应的高度的Item。 因此我们可以计算出这个ListView的总高度,再将它设置到ListView中,那么之前的滚动,高度问题也就不存在了。
代码] 获取并设置ListView高度的方法
01
public
void
setListViewHeightBasedOnChildren(ListView listView) {
02
ListAdapter listAdapter = listView.getAdapter();
03
if
(listAdapter ==
null
) {
04
return
;
05
}
06
07
int
totalHeight =
0
;
08
for
(
int
i =
0
; i < listAdapter.getCount(); i++) {
09
View listItem = listAdapter.getView(i,
null
, listView);
10
listItem.measure(
0
,
0
);
11
totalHeight += listItem.getMeasuredHeight();
12
}
13
14
ViewGroup.LayoutParams params = listView.getLayoutParams();
15
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() -
1
));
16
((MarginLayoutParams)params).setMargins(
10
,
10
,
10
,
10
);
17
listView.setLayoutParams(params);
18
}
[代码] XML布局
01
<ScrollView
02
android:layout_width=
"fill_parent"
03
android:layout_height=
"fill_parent"
04
android:fadingEdge =
"none"
05
android:background=
"#FFF4F4F4"
06
xmlns:android=
"http://schemas.android.com/apk/res/android"
07
>
08
<LinearLayout
09
android:gravity=
"center_horizontal"
10
android:orientation=
"vertical"
11
android:background=
"#fff4f4f4"
12
android:layout_width=
"fill_parent"
13
android:layout_height=
"fill_parent"
14
>
15
<ListView
16
android:id=
"@+id/moreItemsListView"
17
android:layout_width=
"fill_parent"
18
android:layout_height=
"fill_parent"
19
android:cacheColorHint=
"#FFF4F4F4"
20
android:dividerHeight=
"0.0dip"
21
android:fadingEdge=
"none"
22
/>
23
</LinearLayout>
24
</ScrollView>
[图片] scrollview_listview.jpg
- android中scrollview与listview共存
- android中scrollview与listview共存
- android中scrollview与listview共存
- Android:scrollview与listview共存
- listView 与scrollView 共存
- ScrollView与ListView,ExpandableListView共存
- ScrollView与ListView共存代码
- android listview和scrollview共存
- Android ScrollView与ListView,GridView共存冲突解决方案
- ScrollView与ListView,GridView共存冲突解决办法 Android
- Android ScrollView与ListView,GridView共存冲突解决方案
- Android ScrollView与ListView,GridView共存冲突解决方案
- Android ScrollView与ListView,GridView共存冲突解决方案
- Android ScrollView与ListView,GridView共存冲突解决方案
- ScrollView与ListView,GridView共存冲突解决办法 Android
- Android ScrollView与ListView,GridView共存冲突解决方案
- android系列:ScrollView与ListView,GridView共存冲突解决方案
- Android ScrollView与ListView,GridView共存冲突解决方案
- CRT detected that the application wrote to memory after end of heap buffer
- ubuntu 硬盘管理工具
- Java中的值传递和引用传递
- 鼠标事件的监听和使用
- md5加密算法c实现
- android中scrollview与listview共存
- 游走Android系列之Service
- 在.NET使用JSON作为数据交换格式
- 单例模式的4种写法
- 银行业务调度系统
- Windows Socket编程TCP范例之三(WSAAsyncSelect)
- 在64位操作系统上运行32位的程序
- 模运算------x的y次方再模n
- Eclipse 问题:java.lang.ClassNotFoundException: org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAn