Android: finding the rendering height of a ListView element

Is there a way to find the height of the ListView element currently displayed in the list without installing it in the layout XML files?

I tried

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

and although it worked at first, it decided to throw a Null Pointer exception during a call to measure () after starting a new emulator session.

What I'm trying to do is to have an item in a short list (only 2 or 3 full items are visible) to be centered when the program selects.

+7
source share
1 answer

This worked for me:

 public static int getItemViewHeight(Context context, Adapter adapter, int index) { FrameLayout fakeParent = new FrameLayout(context); View view = adapter.getView(index, null, fakeParent); view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED); return view.getMeasuredHeight(); } 
0
source

All Articles