[英]Need max height of ScrollView
如何确定ScrollView可用的最大高度?
我想实现类似ListView的方法,因此想知道有多少空间可用于布局列表项。 考虑一个具有简单LinearLayout的应用程序,其中TestList是ScrollView的子类。 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>
有了这个,我的问题是:我需要多少可见的垂直空间才能将项目添加到“ test_list”。
(我已经走过ListView路由,但是遇到了对getView的过多调用,导致速度降低的问题。由于ListView大大减少了我的使用情况,因此我认为自己可以实现自己的工作,而不是四处逛逛。 ListView问题。)
这是我尝试过的:
1)使用ScrollView.onMeasure的参数-无用,因为它们实际上是无限的。
2)使用ScrollView内部布局的高度-最初为0,因为它没有内容。 即,其高度取决于其内容。
(到目前为止),此功能运行可靠。 关键是在使用高度值之前检查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.