anroid中ScrollView嵌套ListView
来源:互联网 发布:1.12魔兽世界数据库 编辑:程序博客网 时间:2024/05/22 08:05
有时候项目里面需要ScrollView嵌套ListView,但是正常下ListView只会显示一行多一点,解决方法就是填充ListView数据后重新计算ListView的高度,这里有两种方法来实现。
第一种方法:重写ListView
package com.jwzhangjie.test;import android.content.Context;import android.util.AttributeSet;import android.widget.ListView;public class MyListView extends ListView{public MyListView(Context context) {super(context);}public MyListView(Context context, AttributeSet attrs) {super(context, attrs);} @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, expandSpec); }}
第二种方法:添加完数据后计算ListView中所有Item的高度和间隔线的高度然后重新设置ListView的高度
public void setListViewHeightBasedOnChildren(ListView listView) {ListAdapter listAdapter = listView.getAdapter();if (listAdapter == null)return;if (listAdapter.getCount() <= 1)return;int totalHeight = 0;View view = null;for (int i = 0; i < listAdapter.getCount(); i++) {view = listAdapter.getView(i, null, listView);view.measure(0,0);totalHeight += view.getMeasuredHeight();}ViewGroup.LayoutParams params = listView.getLayoutParams();params.height = totalHeight+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));listView.setLayoutParams(params);listView.requestLayout();}
设置完数据后,调用setListViewHeightBasedOnChildren(listview);
注意:ListView的item只能使用LinearLayout包含
- anroid中ScrollView嵌套ListView
- ScrollView中嵌套ListView
- ScrollView中嵌套ListView
- ScrollView中嵌套ListView
- ScrollView中嵌套ListView
- ScrollView中嵌套ListView
- ScrollView中嵌套ListView
- ScrollView中嵌套ListView
- ScrollView中嵌套listview
- ScrollView中嵌套ListView
- ScrollView中嵌套ListView
- ScrollView中嵌套ListView
- ScrollView中嵌套ListView
- ScrollView中嵌套ListView
- ScrollView中嵌套ListView
- ScrollView中嵌套ListView
- ScrollView中嵌套ListView
- ScrollView中嵌套Listview
- STM32 BIT_BAND 位带别名区使用入门
- 细胞
- 国家队ACM论文分类整理(转载)
- 求二叉树中节点的最大距离算法(C)
- 自己训练SVM分类器进行HOG行人检测
- anroid中ScrollView嵌套ListView
- 太极拳透劲的原理推测
- 斐波那契数列算法 Fibonacci Search 优化二分法查找算法
- 用google chart api 在博客中插入公式
- java观察者模式举例
- SVN使用使用方法
- opengl绘制移动的安卓机器人
- AES CTR原理
- LeetCode 解题报告索引