【问题标题】:best practice of using listview with different layout使用具有不同布局的列表视图的最佳实践
【发布时间】: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


【解决方案1】:

尝试使用ListView 来包含所有内容。如果您需要两种类型的行,只需定义一个 Custom ListView 并使用多个自定义 XML 布局来解决不同类型的行。

this tutorial for an example

【讨论】:

  • 你能不能给我一个教程来解释如何使用不同布局的列表视图。
  • 无需创建自定义列表视图来支持多个视图,您的适配器 getView 方法应该简单地返回您需要的视图类型。 Android ListView 和 GridView 在幕后有很多逻辑来确保 convertView 也是正确的类型,因此您甚至不需要传递该功能。
猜你喜欢
  • 2012-10-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多