由于视图测量,布局性能不佳?

时间:2013-07-01 11:11:29

标签: android android-layout android-view

我遇到了性能问题,我认为这是通过测量我的观点引起的。 我的布局是以编程方式构建的,因此我无法显示布局的xml。但是这里有一些可能导致问题的观点:

  • 用于包装所有内容的ScrollView(宽度:MATCH_PARENT,高度:MATCH_PARENT)
  • 包含所有不同内容部分的LinearLayout(宽度:MATCH_PARENT,高度:MATCH_PARENT,上一个视图的子项)
  • 异步填充的两个LinearListViews(LinearLayout)(宽度:MATCH_PARENT,高度:WRAP_CONTENT,上一个视图的子节点)

当我填充LinearListView时,似乎只会出现问题;添加视图似乎会多次触发View.measure:

Method profiling result

为什么View.measured多次调用它?具有MATH_PARENT的LinearLayout的大小意味着它不需要在添加子项时再次测量它,对吗?

我不确定如何防止这种情况发生,或者如何调试问题。我已经尝试将LinearListView的可见性设置为GONE,直到添加了所有项目,但这没有什么区别。

我希望我提供足够的信息让某人指出我正确的方向。

编辑#1(07/01):

以下是用于LinearListViews的适配器基类的一些部分:

public abstract class DataProviderAdapter<T> extends BaseAdapter
{
    ...

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        View row = convertView;
        T item = getItem(position);

        Log.d(TAG, "getView(): Position: " + position + ", ConvertView: "
                + (convertView == null ? "null" : convertView) + ", TotalCount: " + getCount());

        if (row == null) {
            row = buildRow(position);
            markRowAsLoading(row);
        }

        if (item == null) {

            if (convertView != null) {
                markRowAsLoading(row);
            }

            int skip;
            if (itemsRequestParams != null) {
                int pageSize = itemsRequestParams.getTake();

                // determine how many items to skip
                skip = position - (position % pageSize);
            }
            else {
                skip = 0;
            }

            // if the page isn't already being fetched
            if (!pagesReceiving.contains(skip)) {
                pagesReceiving.add(skip);
                AsyncTaskUtil.executeMultiThreaded(new ReceiveItemsTask(), skip);
            }
        }
        else {
            Log.d(TAG, getClass().getSimpleName() + " - PrepareRow: " + getItemId(position)
                    + ", Pos: " + position);
            prepareRow(row, item, position);
        }
        return row;
    }

    protected void processDataResult(int skip, DataResult<T> result)
    {
        if (result instanceof GroupedDataResult<?>) {
            GroupedDataResult<T> groupedResult = (GroupedDataResult<T>)result;
            totalItemCount = groupedResult.getTotalGroupCount() + groupedResult.getTotalItemCount();
        }
        else {
            totalItemCount = result.getTotalItemCount();
        }

        for (int i = 0; i < result.getItems().size(); i++) {
            itemList.put(skip + i, result.getItems().get(i));
        }

        Log.d(TAG, "processDataResult(" + skip + ")");
        notifyDataSetChanged();
    }

    static protected class CellHolder
    {
        public ProgressBar ProgressSpinner;
        public List<android.widget.ImageView> Images;
        public List<TextView> Labels;
        private final HashMap<String, Object> _extras = new HashMap<String, Object>();

        public void putExtra(String key, Object value)
        {
            _extras.put(key, value);
        }

        @SuppressWarnings("unchecked")
        public <T> T getExtra(String key)
        {
            return _extras.containsKey(key) ? (T)_extras.get(key) : null;
        }
    }

    ...

    private class ReceiveItemsTask extends AsyncTask<Integer, Void, DataResult<T>>
    {
        private int skip;

        @Override
        protected DataResult<T> doInBackground(Integer... params)
        {
            skip = params[0];
            DataResult<T> items = getItems(skip);
            Log.d(TAG, "doInBackground(" + skip + ")");
            return items;
        }

        @Override
        protected void onPostExecute(DataResult<T> result)
        {
            Log.d(TAG,
                    "onPostExecute("
                            + (result == null ? "null" : result.getItems().size() + "/"
                                    + result.getTotalItemCount()) + ")");
            if (result == null) {
                return;
            }
            processDataResult(skip, result);
            Log.d(TAG, "onPostExecute end");
        }
    }
}

编辑#2(07/01):

我将RelativeLayout子类化并覆盖了onMeasure,因此我可以记录正在测量的视图以及按哪种顺序。 结果如下:

07-01 15:37:37.434: V/DefaultLayout(8172): DefaultLayout.onMeasure
07-01 15:37:37.434: V/DefaultLayout(8172): TabView.onMeasure
07-01 15:37:37.434: V/DefaultLayout(8172): ItemDetailsContainerView.onMeasure
07-01 15:37:37.438: V/DefaultLayout(8172): HeaderView.onMeasure
07-01 15:37:37.438: V/DefaultLayout(8172): ItemDetailsInfoFieldsLayout.onMeasure
07-01 15:37:37.438: V/DefaultLayout(8172): ItemReviewTableView.onMeasure
07-01 15:37:37.438: V/DefaultLayout(8172): InfoFieldsLayout.onMeasure
07-01 15:37:37.438: V/DefaultLayout(8172): InfoFieldsLayout.onMeasure
07-01 15:37:37.442: V/DefaultLayout(8172): InfoFieldsLayout.onMeasure
07-01 15:37:37.442: V/DefaultLayout(8172): InfoFieldsLayout.onMeasure
07-01 15:37:37.442: V/DefaultLayout(8172): InfoFieldsLayout.onMeasure
07-01 15:37:37.442: V/DefaultLayout(8172): InfoFieldsLayout.onMeasure
07-01 15:37:37.442: V/DefaultLayout(8172): InfoFieldsLayout.onMeasure
07-01 15:37:37.442: V/DefaultLayout(8172): InfoFieldsLayout.onMeasure
07-01 15:37:37.446: V/DefaultLayout(8172): ItemReviewTableView.onMeasure
07-01 15:37:37.446: V/DefaultLayout(8172): InfoFieldsLayout.onMeasure
07-01 15:37:37.446: V/DefaultLayout(8172): InfoFieldsLayout.onMeasure
07-01 15:37:37.446: V/DefaultLayout(8172): InfoFieldsLayout.onMeasure
07-01 15:37:37.446: V/DefaultLayout(8172): InfoFieldsLayout.onMeasure
07-01 15:37:37.450: V/DefaultLayout(8172): InfoFieldsLayout.onMeasure
07-01 15:37:37.450: V/DefaultLayout(8172): InfoFieldsLayout.onMeasure
07-01 15:37:37.450: V/DefaultLayout(8172): InfoFieldsLayout.onMeasure
07-01 15:37:37.450: V/DefaultLayout(8172): InfoFieldsLayout.onMeasure
07-01 15:37:37.450: V/DefaultLayout(8172): RelatedItemsTableView.onMeasure
07-01 15:37:37.454: V/DefaultLayout(8172): RatingView.onMeasure
07-01 15:37:37.458: V/DefaultLayout(8172): RatingView.onMeasure
07-01 15:37:37.462: V/DefaultLayout(8172): RatingView.onMeasure
07-01 15:37:37.466: V/DefaultLayout(8172): RatingView.onMeasure
07-01 15:37:37.470: V/DefaultLayout(8172): RatingView.onMeasure
07-01 15:37:37.474: V/DefaultLayout(8172): RatingView.onMeasure
07-01 15:37:37.478: V/DefaultLayout(8172): RatingView.onMeasure
07-01 15:37:37.478: V/DefaultLayout(8172): RatingView.onMeasure
07-01 15:37:37.482: V/DefaultLayout(8172): RatingView.onMeasure
07-01 15:37:37.482: V/DefaultLayout(8172): RatingView.onMeasure
07-01 15:37:37.486: V/DefaultLayout(8172): RatingView.onMeasure
07-01 15:37:37.486: V/DefaultLayout(8172): RatingView.onMeasure
07-01 15:37:37.490: V/DefaultLayout(8172): RatingView.onMeasure
07-01 15:37:37.490: V/DefaultLayout(8172): RatingView.onMeasure
07-01 15:37:37.494: V/DefaultLayout(8172): RatingView.onMeasure
07-01 15:37:37.494: V/DefaultLayout(8172): RatingView.onMeasure
07-01 15:37:37.498: V/DefaultLayout(8172): RelatedItemsTableView.onMeasure
07-01 15:37:37.498: V/DefaultLayout(8172): RatingView.onMeasure
07-01 15:37:37.498: V/DefaultLayout(8172): RatingView.onMeasure
07-01 15:37:37.502: V/DefaultLayout(8172): RatingView.onMeasure
07-01 15:37:37.502: V/DefaultLayout(8172): RatingView.onMeasure
07-01 15:37:37.506: V/DefaultLayout(8172): RatingView.onMeasure
07-01 15:37:37.506: V/DefaultLayout(8172): RatingView.onMeasure
07-01 15:37:37.510: V/DefaultLayout(8172): RatingView.onMeasure
07-01 15:37:37.510: V/DefaultLayout(8172): RatingView.onMeasure
07-01 15:37:37.514: V/DefaultLayout(8172): RatingView.onMeasure
07-01 15:37:37.514: V/DefaultLayout(8172): RatingView.onMeasure
07-01 15:37:37.518: V/DefaultLayout(8172): RatingView.onMeasure
07-01 15:37:37.518: V/DefaultLayout(8172): RatingView.onMeasure
07-01 15:37:37.522: V/DefaultLayout(8172): RatingView.onMeasure
07-01 15:37:37.522: V/DefaultLayout(8172): RatingView.onMeasure
07-01 15:37:37.526: V/DefaultLayout(8172): RatingView.onMeasure
07-01 15:37:37.526: V/DefaultLayout(8172): RatingView.onMeasure
...

此列表会持续一段时间。但对我来说似乎很奇怪的是,它首先测量顶视图(DefaultLayout.onMeasure),然后测量所有子项(所以所有视图)。它多次这样做!

什么可能导致在顶视图上调用onMeasure?

编辑#3(07/02):

可能notifyDataSetChanged()触发了顶视图的测量吗?

编辑#4(07/03):

是的,似乎notifyDataSetChanged()触发了要测量的顶级视图(我能够在新的测试项目中重现这一点)。由于我的适配器的工作方式,notifyDataSetChanged()被多次调用。

当我致电notifyDataSetChanged()时,衡量所有观看次数的原因是什么?有没有办法阻止这种情况发生?

2 个答案:

答案 0 :(得分:4)

您没有说出相对布局的位置,这是列表中的包装吗?

根据Romain Guy的说法,相对布局总是会进行两次测量,所以这可能会导致一些减速。你有机会嵌套相对布局吗?显然,这可能具有指数时间复杂性。

请参阅此答案以供参考(以及他实际上说的Google io视频的链接):

https://stackoverflow.com/a/17496262/1281144

答案 1 :(得分:0)

在你的描述中直接引起我注意的一件事是你的LinearLayout,你的ScrollView的直接(和唯一)子节点被设置为高度为MATCH_PARENT - 这是错误的 - 它的高度应该是WRAP_CONTENT。 ScrollView的子节点与ScrollView本身的高度不同 - 否则无法滚动。

除此之外,查看LinearListView的代码,我看到了:

private DataSetObserver mDataObserver = new DataSetObserver() {

    @Override
    public void onChanged() {
        setupChildren();
    }

    @Override
    public void onInvalidated() {
        setupChildren();
    }

};

因此,每当底层数据集发生变化时,都会调用setupChildren(),删除所有视图并重绘它们。

在processDataResult()方法中,您可以这样做:

 for (int i = 0; i < result.getItems().size(); i++) {
        itemList.put(skip + i, result.getItems().get(i));
    }

因此,在添加每个项目后,列表正在重新绘制(当然在UI线程上)。

我会自己修改库以删除onChanged()中对setupChildren()的调用,如下所示:

private DataSetObserver mDataObserver = new DataSetObserver() {

    @Override
    public void onChanged() {
        //setupChildren(); do nothing here
    }

    @Override
    public void onInvalidated() {
        setupChildren();
    }

};

我认为在调用notifyDataSetChanged()时会调用onInvalidated()