我目前正在尝试在ScrollView中使用ListView.我知道从我读过的内容中看到了这一点,但是我试图通过显示所有行来完全展开ListView,因此不需要滚动它.然而,我一直在努力告诉ListView完全展开以显示它的所有行,因为它需要一个定义的高度.有没有人知道在绘制之前计算完全展开的ListView的高度的方法?
此问题主要源于您无法将可滚动视图放在另一个可滚动视图中.我很好,因为ListView将无法滚动,只要我可以展开它以显示其所有行.但是,我无法做到这一点,但却无法给它定义一个高度,这似乎是我需要计算的.
请参阅下面的URL以获取草图(我是新用户,所以我不允许发布一个).它表明我的完整布局对于"物理"屏幕来说太大了,需要滚动以显示列表的其余部分和底部的按钮.我试图弄清楚即使没有ListView,"虚拟"屏幕也太大而无法放在一个屏幕上.
net*_*ist 56
好吧,多亏了鲁迪,他的建议非常有帮助.以下是它的实现方式.
1)创建一个扩展ListView的新类:
package com.example.android.views;
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.widget.ListView;
public class ExpandedListView extends ListView {
private android.view.ViewGroup.LayoutParams params;
private int old_count = 0;
public ExpandedListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onDraw(Canvas canvas) {
if (getCount() != old_count) {
old_count = getCount();
params = getLayoutParams();
params.height = getCount() * (old_count > 0 ? getChildAt(0).getHeight() : 0);
setLayoutParams(params);
}
super.onDraw(canvas);
}
}
Run Code Online (Sandbox Code Playgroud)
2)...最后将新视图添加到xml布局文件中:
<com.example.android.views.ExpandedListView
android:id="@+id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="none"
android:padding="0px"
/>
Run Code Online (Sandbox Code Playgroud)
Rom*_*iel 14
计算高度的正确方法正是:
int height = 0;
for (int i = 0; i < listView.getChildCount(); i++) {
height += listView.getChildAt(i).getMeasuredHeight();
height += listView.getDividerHeight();
}
Run Code Online (Sandbox Code Playgroud)
alc*_*san 11
注意!正确的计算高度的方法不是
params.height = getCount() * (old_count > 0 ? getChildAt(0).getHeight() : 0);
Run Code Online (Sandbox Code Playgroud)
我们必须增加每个(减1)分频器高度的高度.
if(oldCount > 0 && getCount() > 0)
params.height = getCount()
* (getChildAt(0).getHeight() + getDividerHeight())
- getDividerHeight();
else
params.height = 0;
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
38981 次 |
最近记录: |