【发布时间】:2014-09-11 14:14:34
【问题描述】:
我遇到了一个烦人的问题:
我有一个选项卡主机,在其中我添加了一个视图寻呼机 在视图寻呼机里面有三个页面
在第一页中,我必须在彼此下方添加两个列表视图以及其他一些控件,因为我知道在搜索时我不能这样做,因为在大量搜索之后,我需要为所有视图滚动我发现有些人建议自己计算列表大小并在布局参数中更改列表高度,我使用以下代码:
public class NonScrollableListView extends ListView {
private android.view.ViewGroup.LayoutParams params;
private int old_count = 0;
private int item_height = -1;
private int divider_height = -1;
private boolean isDrawn = false;
public NonScrollableListView(Context context) { super(context); }
public NonScrollableListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public NonScrollableListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onDraw(Canvas canvas) {
if (isDrawn == false) {
if (item_height == -1 && getCount() > 0 && getChildAt(0) != null) {
item_height = getChildAt(0).getHeight();
}
if (divider_height == -1 && getCount() > 0 && getChildAt(0) != null) {
divider_height = getDividerHeight();
}
Log.i("YARAB", ""+ canvas);
Log.i("YARAB", ""+ getCount());
if (getCount() != old_count) {
old_count = getCount();
params = getLayoutParams();
params.height = (getCount() * item_height) + (getCount() * divider_height);
setLayoutParams(params);
}
isDrawn = true;
}
super.onDraw(canvas);
}
}
它工作正常并计算列表高度,但现在渲染我的列表和在片段之间交换需要很多时间,任何人都可以在这里提供帮助。
【问题讨论】:
-
您可以使用 OnScrollListener 计算垂直滚动时的列表高度
-
在将其显示到屏幕之前,我已经在 onDraw 中进行了计算,因为我在滚动视图中添加了我的列表视图。
-
我不确定我是否理解您的问题,但
NonScrollableListView听起来不对。如果您不想滚动,那为什么还要使用ListView? -
我希望能够在滚动视图中添加两个列表视图并绘制列表视图的所有项目并使滚动仅适用于滚动视图。
-
@AmiraElsayedIsmail 这是不可能的,你不能嵌套可滚动的
Views。有什么特别的原因为什么你不只在一个大的ListView中显示所有内容吗?
标签: android performance listview android-fragments scrollview