0

How can I determine the maximum height available to a ScrollView?

I would like to implement something like a ListView and, thus, would like to know how much space is available to layout list items. Consider an app with a simple LinearLayout, where TestList is a subclassed ScrollView. TestList contains a single LinearLayout, added at runtime.

<LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="top" />

    <com.example.testsimplelist.TestList
        android:id="@+id/test_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="bottom" />

</LinearLayout>

With this, my question is: how much visible vertical space do I have for adding items to "test_list".

(I've already gone the ListView route, but am running into problems with excessive calls to getView, causing too much speed degregation. Since my usage case is much reduced from ListView, I figure I can implement my own for less work than hacking around ListView problems.)

Here's what I've tried:

1) using the params of ScrollView.onMeasure - useless because they are effectively infinite.

2) using the height of the layout inside the ScrollView - this is initially 0 since it has no contents. That is, its height is dependent on its contents.

4

1 回答 1

1

这是可靠的(到目前为止)。关键是在使用高度值之前检查MeasureSpec模式:

@Override
protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec)
{
  super.onMeasure (widthMeasureSpec, heightMeasureSpec);

  int heightMode = MeasureSpec.getMode(heightMeasureSpec);
  if (heightMode == MeasureSpec.EXACTLY)
  {
    int heightSpec = getMeasuredHeight();
    fillView (heightSpec);
  }
}
于 2013-06-28T14:57:09.770 回答