不滚动的listView(解决带head foot 计算)

package test.test;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.ListAdapter;
import android.widget.ListView;

/**
* 不滚动的listView 即全部显示
*
* @author david
* @deprecated
* @see ComponentUtils.setListViewHeightBasedOnChildren
*/

@Deprecated
public class NoScrollListView extends ListView {

public NoScrollListView(Context paramContext) {
super(paramContext);
}

public NoScrollListView(Context paramContext, AttributeSet paramAttributeSet) {
super(paramContext, paramAttributeSet);
}

public NoScrollListView(Context paramContext,
AttributeSet paramAttributeSet, int paramInt) {
super(paramContext, paramAttributeSet, paramInt);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

super.onMeasure(widthMeasureSpec,
MeasureSpec.makeMeasureSpec(MeasureSpec.UNSPECIFIED, 0));

ListAdapter listAdapter = getAdapter();

if (listAdapter == null) {
return;
}
int totalHeight = 0;
int height;

int footCount = getFooterViewsCount();
int headCount = getHeaderViewsCount();
int adapterCount = listAdapter.getCount();
int childCount = getChildCount();
Log.e("~~~~", "getChildCount():" + childCount + ",getAdapterCount():"
+ adapterCount);
Log.e("~~~~", "footCount:" + footCount + ",headCount:" + headCount);
for (int j = 0; j < childCount; j++) {
Log.e("all child ", "child :" + j + ",height:"
+ getChildAt(j).getHeight());
}
for (int i = 0; i < adapterCount; i++) { // listAdapter.getCount()返回数据项的数目
View listItem = null;
if (childCount > 0) // 只有childCount 存在时候才使用已有的子件直接计算
{

if (footCount > 0 && i >= adapterCount - footCount) {// 获取listItem
// footer部分
listItem = getChildAt((i - adapterCount) + childCount);
} else if (i >= childCount - footCount)
listItem = getChildAt(i + headCount + footCount); // 在listView内foothead的子项index都是前面的
else
listItem = getChildAt(i);
}
height = 0;
if (listItem == null) {
listItem = listAdapter.getView(i, null, this);
try {
listItem.measure(MeasureSpec.UNSPECIFIED,
MeasureSpec.UNSPECIFIED); // 计算子项View 的宽高
height = listItem.getMeasuredHeight();
} catch (Throwable t) {
t.printStackTrace();
}
} else
height = listItem.getHeight();

Log.v("TAG", "listAdapter row " + i + "height:" + height);

totalHeight += height; // 统计所有子项的总高度

}
// 总高度 每个item总高度 + 子项间隔总和 +头部缩进 + 底部缩进
totalHeight = totalHeight
+ (getDividerHeight() * (listAdapter.getCount() - 1))
+ getPaddingBottom() + getPaddingTop();
// Log.v(TAG, "params.height" + params.height);
// // listView.getDividerHeight()获取子项间分隔符占用的高度
//
// // params.height最后得到整个ListView完整显示需要的高度

setMeasuredDimension(getMeasuredWidth(), totalHeight);

}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值