简体   繁体   English

需要ScrollView的最大高度

[英]Need max height of ScrollView

How can I determine the maximum height available to a ScrollView? 如何确定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. 我想实现类似ListView的方法,因此想知道有多少空间可用于布局列表项。 Consider an app with a simple LinearLayout, where TestList is a subclassed ScrollView. 考虑一个具有简单LinearLayout的应用程序,其中TestList是ScrollView的子类。 TestList contains a single LinearLayout, added at runtime. TestList包含单个LinearLayout,在运行时添加。

<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". 有了这个,我的问题是:我需要多少可见的垂直空间才能将项目添加到“ 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.) (我已经走过ListView路由,但是遇到了对getView的过多调用,导致速度降低的问题。由于ListView大大减少了我的使用情况,因此我认为自己可以实现自己的工作,而不是四处逛逛。 ListView问题。)

Here's what I've tried: 这是我尝试过的:

1) using the params of ScrollView.onMeasure - useless because they are effectively infinite. 1)使用ScrollView.onMeasure的参数-无用,因为它们实际上是无限的。

2) using the height of the layout inside the ScrollView - this is initially 0 since it has no contents. 2)使用ScrollView内部布局的高度-最初为0,因为它没有内容。 That is, its height is dependent on its contents. 即,其高度取决于其内容。

This is working reliably (so far). (到目前为止),此功能运行可靠。 The key is to check the MeasureSpec mode before using the height value: 关键是在使用高度值之前检查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);
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM