ListView(ScrollView)嵌套ListView、GridView显示不全,内容大于一行计算的高度不正确的解决办法
2016-01-13 17:24
841 查看
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">相信很多人都遇到过ListView或是ScrollView嵌套ListView,ListView(ScrollView)嵌套GridView,内容只显示一行,这个问题网上已有解决办法。</span>
1、对于[b]ListView或是ScrollView嵌套ListView,一般是[/b]
利用Utility 类的静态方法setListViewHeightBasedOnChildren()即可实现:
在listview.setAdapter()之后调用Utility.setListViewHeightBasedOnChilren(listview)就能正常显示了。
public class LvHeightUtil { public static void setListViewHeightBasedOnChildren(ListView listView) { ListAdapter listAdapter = listView.getAdapter(); if (listAdapter == null) { 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)) +10; listView.setLayoutParams(params); } }
<span style="font-family:SimSun;"></span>
2、对于ListView或是ScrollView嵌套GridView,一般是:
<pre name="code" class="java">public class NoScrollGridView extends GridView { public NoScrollGridView(Context context) { super(context); } public NoScrollGridView(Context context, AttributeSet attrs) { super(context, attrs); } public NoScrollGridView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, expandSpec); } }
3、对于嵌套的ListView内容如果是TextView内容大于一行显示不全的,则ListView的高度就会计算错误,它只计算了一行的高度,对于TextView可重写它的onMeasure方法,计算高度后,再去重新set
public class ShareAppendixTextView extends TextView { private Context context; public ShareAppendixTextView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); this.context = context; } public ShareAppendixTextView(Context context, AttributeSet attrs) { super(context, attrs); this.context = context; } public ShareAppendixTextView(Context context) { super(context); this.context = context; } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); Layout layout = getLayout(); if (layout != null) { int height = (int)Math.ceil(getMaxLineHeight(this.getText().toString())) + getCompoundPaddingTop() + getCompoundPaddingBottom(); int width = getMeasuredWidth(); setMeasuredDimension(width, height); } } private float getMaxLineHeight(String str) { float height = 0.0f; float screenW = ((Activity)context).getWindowManager().getDefaultDisplay().getWidth(); float paddingLeft = ((LinearLayout)this.getParent()).getPaddingLeft(); float paddingReft = ((LinearLayout)this.getParent()).getPaddingRight(); //这里具体this.getPaint()要注意使用,要看你的TextView在什么位置,这个是拿TextView父控件的Padding的,为了更准确的算出换行 int line = (int) Math.ceil( (this.getPaint().measureText(str)/(screenW-paddingLeft-paddingReft))); height = (this.getPaint().getFontMetrics().descent-this.getPaint().getFontMetrics().ascent)*line; return height; } }
通过这样,ListView嵌套ListView,显示多行的问题就能得到解决,上一张解决的图片:
相关文章推荐
- eclipse注释模板
- 【Objective-C学习记录】第四十二天
- 文件的基础
- Linux 软连接 & 硬链接
- SQL_3_表达式、条件语句与运算
- [转] Markdown
- Linux Study之--通过udev创建ASM DISK(RAC共享存储)
- 访问google的python脚本
- NSUserDefaults
- poj-3687-Labeling Balls
- 自定义的分割线
- 你不可不知的HTML优化技巧
- alert 替代效果smoke.js
- JSON数据理解
- [转] 最大似然估计
- Android实现上拉加载更多以及下拉刷新功能(ListView)
- 用二进制,十进制,输出一个随机数
- python程序--实现zip压缩
- 基于LBS的兴趣点查询
- SharedPrefs工具类