我想在渲染之前测量ListView
中每个项目的高度。测量列表视图项的高度
我加入这个方法我自定义ListView
public void measureItemsHeight() {
mItemCount = getAdapter().getCount();
if(mItemOffsetY == null){
mItemOffsetY = new int[mItemCount];
}
for(int i = 0; i < mItemCount; ++i){
View view = getAdapter().getView(i, null, this);
view.measure(0, 0);
mItemHeight[i] = view.getMeasuredHeight();
}
}
然后,在我的活动的onCreate()
:
mListView.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
mListView.measureItemsHeight();
}
}
);
值我得到的是比getHeight()
报道的有所不同,如getMeasuredHeight()
返回48 ,并且getHeight()
返回72.
The layo我使用的项目是simple_list_item
它在我看来这就像一个hdpi和xhdpi问题,有没有办法解决这个问题?我用来调试的设备是Samsung SGS2,可能是这个问题。
你可以附加'simple_list_item.xml'吗?另外,你可以给一些*上下文*,解释你为什么要测量所有可能的孩子的身高? – Raffaele
以及我需要得到我的项目的高度,使自己的ScrollY德获得多少像素,我已经在列表视图中滚动,因为你不能跟踪通常 –