I want to measure the height of a ListView
and expand it after loading some data. The ListAdapter choose different layouts for the items depending on the type of the data.
I tried this code:
public class ExpandedListView extends ListView {
[...]
@Override
protected void onDraw(Canvas canvas) {
if (getCount() != 0) {
params = getLayoutParams();
int height = 0;
for (int i = 0; i < getCount(); i++) {
height += getChildAt(i).getMeasuredHeight();
height += getDividerHeight();
}
params.height = height;
setLayoutParams(params);
}
super.onDraw(canvas);
}
[...]
But I get a NullPointerException for getChildAt(i)
07-29 09:48:08.745: E/AndroidRuntime(21298): FATAL EXCEPTION: main
07-29 09:48:08.745: E/AndroidRuntime(21298): java.lang.NullPointerException
07-29 09:48:08.745: E/AndroidRuntime(21298): at com.app.ExpandedListView.onDraw(ExpandedListView.java:24)
07-29 09:48:08.745: E/AndroidRuntime(21298): at android.view.View.draw(View.java:13719)
Only one child is accessible, but getCount() gives me the value 38. Some other solutions for this problem multiply the getCount() with the height of the first Child at 0. But my items have different heights.
//params.height = getCount() * (old_count > 0 ? getChildAt(0).getHeight() : 0);
That's no solution for me. How can I receive the real height of my ListView?
EDIT:
For better understanding:
I've got a Parallax-Effect in my Activity. A ScrollView contains an ImageView as a Header and below a ListView. The ImageView scrolls a little bit slower than the ListView. So I can't use a normal ListView because of the ScrollView-Parent.
A LinearLayout is no option because it behaves in combination with TextViews wrong and all TextViews are 2 lines tall maximum.