Sky*_*net 5 android listview android-view android-viewpager
我有一个片段,我用TabLayout托管了一个ViewPager,ViewPager的内容是一个包含ListView的片段.
我知道我的ViewPager无法使用wrap_content,开始时的高度没有计算.
所以我这样做了:
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
boolean wrapHeight = MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.AT_MOST;
final View tab = getChildAt(0);
if (tab == null) {
return;
}
int width = getMeasuredWidth();
if (wrapHeight) {
// Keep the current measured width.
widthMeasureSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY);
}
Fragment fragment = ((Fragment) getAdapter().instantiateItem(this, getCurrentItem()));
heightMeasureSpec = getMeasureExactly(fragment.getView(), widthMeasureSpec);
//Log.i(Constants.TAG, "item :" + getCurrentItem() + "|height" + heightMeasureSpec);
// super has to be called again so the new specs are treated as
// exact measurements.
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
int getMeasureExactly(View child, int widthMeasureSpec) {
child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
int height = calculateHeight ((ListView)child.findViewById(R.id.list)); //getMeasuredHeight();
System.out.println("Height is: "+height);
return MeasureSpec.makeMeasureSpec(height , MeasureSpec.EXACTLY);
}
private int calculateHeight(ListView list) {
int height = 0;
for (int i = 0; i <10; i++) {
View childView = list.getAdapter().getView(i, null, list);
childView.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
height+= childView.getMeasuredHeight();
}
//dividers height
// height += list.getDividerHeight() * 11; //list.getCount();
return height;
}
Run Code Online (Sandbox Code Playgroud)
我得到一个包含10行的List来填充,但只有9个可见,一个移出视图.
这里的问题是如何精确测量列表行高度并将其添加到10个项目中.
这个问题一直困扰着stackoverflow,但似乎没有具体的解决方案.特别是对于ListView孩子.因为我可以使用TableLayout.
我也知道这样做的记忆和滚动因素,但这是设计的,我无能为力.
上面代码的要点是传递一行列表的高度,然后乘以10(因为我只需要列表中的10行+分隔符高度)
| 归档时间: |
|
| 查看次数: |
323 次 |
| 最近记录: |