Android:查找ListView的项目渲染高度

前端之家收集整理的这篇文章主要介绍了Android:查找ListView的项目渲染高度前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
有没有办法找到当前显示在列表中的ListView项目的高度而不在布局xml文件中自己设置它?

我试过了

@H_404_4@View listItem = listAdapter.getView(currentIndex,null,listView); listItem.measure(MeasureSpec.EXACTLY,MeasureSpec.UNSPECIFIED); int itemHeight = listItem.getMeasuredHeight();

虽然它最初工作,但它决定在启动一个新的仿真器会话后在measure()调用期间抛出一个Null Pointer Exception.

我正在尝试做的是在程序选择时将短项列表中的项目(只有2或3个完整项目可见)置于中心位置.

解决方法

这对我有用: @H_404_4@public static int getItemViewHeight(Context context,Adapter adapter,int index) { FrameLayout fakeParent = new FrameLayout(context); View view = adapter.getView(index,fakeParent); view.measure(View.MeasureSpec.UNSPECIFIED,View.MeasureSpec.UNSPECIFIED); return view.getMeasuredHeight(); }

猜你在找的Android相关文章