好,直接撸代码,这样更明了
1.关于总体测量
当使用ScrollView嵌套ListView的时候,如果不对ListView进行高度的测量,ListView也不知道自己到底有多长,所以ListView会显示不全的,这里先上总体测量ListView的方法(这种测量方式有很多中,这里就上一种了);
public void setListViewHeight(ListView listView) { // 获取ListView对应的Adapter ListAdapter listAdapter = listView.getAdapter(); if (listAdapter == null) { return; } int totalHeight = 0; for (int i = 0, len = listAdapter.getCount(); i < len; i++) { // listAdapter.getCount()返回数据项的数目 View listItem = listAdapter.getView(i, null, listView); listItem.measure(0, 0); // 计算子项View 的宽高 totalHeight += listItem.getMeasuredHeight(); // 统计所有子项的总高度 } ViewGroup.LayoutParams params = listView.getLayoutParams(); params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)); listView.setLayoutParams(params); }代码很简单,先拿到ListView的adapter,通过adapter循环拿到每个itemview,然后计算每个itemview的高度再叠加起来,再加上分割线的高度。
这种测量方式在一般的情况下是没有问题的,测量的高度也好,显示也完整,但是在项目中的时候itemview的布局会很复杂,当有多行的TextView的时候着个测量方法就没有什么效果了,ListView显示就不完整了
2.完整的测量方式
当在itemview的布局里面有多行的textview的时候,itemview自身测量的高度是不准确的,它并不知道如何去测量这个多行的textview,所以这时候就要求我们帮助他来完成这个多行的textview的测量,重写一个textview的自定义类就行了
写一个类继承自TextView,重写它的onMeasure方法(
这里参考了其他大神的测量方法)
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); Layout layout = getLayout(); if (layout != null) { int height = (int) FloatMath.ceil(getMaxLineHeight(this.getText().toString())) + getCompoundPaddingTop() + getCompoundPaddingBottom(); int width = getMeasuredWidth(); setMeasuredDimension(width, height); } } private float getMaxLineHeight(String str) { float screenW = ((Activity)getContext()).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))); float height = (this.getPaint().getFontMetrics().descent-this.getPaint().getFontMetrics().ascent)*line; return height; }
将itemview中的textview替换掉,然后再配合上上一种的测量方法,大部分的测量都不会出问题的。