android - Android 设置 ListView 高度依赖于项目高度
问题描述
我有一个 ListView 与高度总是不同的项目。但我不想计算 ListView 的总高度,因为 ListView 高度应该取决于项目。
我在 ListView 中添加了这个:
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
ListAdapter listAdapter = getAdapter();
if (listAdapter == null) {
return;
}
int totalHeight = 0;
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, JOListView.this);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = getLayoutParams();
if (withTollerance) {
params.height = Math.round(totalHeight * 1.05f);
} else {
params.height = totalHeight;
}
setLayoutParams(params);
requestLayout();
}
它没有给我真正的高度。有谁知道如何获得每件物品的正确高度?
解决方案
推荐阅读
- amazon-web-services - 如何在终端中编辑 AWS 凭证?
- python - Scikit learn + Pandas ValueError:形状(1,1)和(10,10)未对齐
- ruby - 这个 SQL 调试输出来自哪里?
- reactjs - ReactJS(Typescript + Fabric UI) - 状态或数据处理程序?
- nativescript - 导航返回时 Nativescript Webviewinterface 回调不起作用
- java - 程序查找重复 n 个字符的字符串中“a”的数量?
- alpha-vantage - Alpha vantage - 如何获得特定日期的股票 SMA
- apache-zookeeper - 正确处理 CuratorFramework 关闭方法
- ios - 更改 UICollectionView 的反弹起点/终点
- amazon-web-services - 如何在 API Gateway 中检查 Cognito 权限