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.

Peri Hartman
  • 17,906
  • 17
  • 49
  • 85
  • "*but am running into problems with excessive calls to getView, causing too much speed degregation*" - that sounds like you're doing something very wrong; `ListView`s are built for speed. Regarding 1): that's because the params are not plain `int`s, but encoded as per `View.MeasureSpec`. Definitely have a read through the [Custom Components guide](http://developer.android.com/guide/topics/ui/custom-components.html). – MH. Jun 25 '13 at 23:01
  • speed degredation - I agree with you. However, while the ListView itself is probably efficient, something wrapping it was causing it to layout out multiple times and incrementally. So, the effect was to call getView for one item, recycle it, call getView for the second item, recycle it, and so on. It did this twice before calling a 3rd succession of getView to fill the list. Even if I were to discover the true cause, it might be fragile - my fix might not be independent of future Android changes. Who knows... – Peri Hartman Jun 25 '13 at 23:58
  • It appears that ScrollView.getMeasuredHeight() is working. But I also needed to set the TestList widget weight to 1 (everything else 0). – Peri Hartman Jun 26 '13 at 01:02

1 Answers1

1

This is working reliably (so far). The key is to check the MeasureSpec mode before using the height value:

@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);
  }
}
Peri Hartman
  • 17,906
  • 17
  • 49
  • 85
  • What does this code do exactly? Can I make it so that scrollview will be given with a "max height" variable, so that it can be wrap-content for its height and take as much height as the child needs, but not more than this size? – android developer Apr 10 '14 at 08:53
  • I actually used ViewGroup, not a ScrollView. It's very expensive to create child views so, if you have a long list, you only want to create enough views for what is currently displayed and "recycle" them as the user scrolls. Not sure what you mean by "wrap-content for its height". – Peri Hartman Apr 10 '14 at 14:34
  • I'm well known with AdapterViews (like ListViews) and how it works for them. I ask about something else. Here's a post I've written about it: http://stackoverflow.com/questions/22984740/how-to-set-a-max-height-of-a-any-kind-of-view . hope you understand. – android developer Apr 10 '14 at 17:14