使用不同布局的listview的最佳实践

时间:2014-09-11 14:14:35

标签: android performance listview android-fragments scrollview

我正面临一个恼人的问题:

我有一个标签主机,在其中我添加了一个视图寻呼机 视图寻呼机内有三页

在第一页中,我必须在下面添加两个列表视图以及其他一些控件,因为我知道搜索我不能这样做,因为我需要滚动来查看所有视图,经过很多搜索我发现有些人建议自己计算列表大小并在布局参数中更改列表高度,我使用以下代码:

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);
    }
}

它工作正常并计算列表高度,但现在需要花费大量时间来渲染我的列表并在片段之间交换,任何人都可以在这里帮忙。

1 个答案:

答案 0 :(得分:0)

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

请参阅this tutorial for an example

相关问题