ListView测量会导致常量getView(..)请求,从而减慢设备速度

时间:2011-08-22 15:07:27

标签: java android listview

这里描述的问题是在3.0以及3.1上遇到的 宏碁Iconia A500。片段中使用以下布局(不是 ListFragment)。不知何故,它导致ListView,添加到 fragment_swipeview_lr_leftview_body(和 要分别测量fragment_swipeview_lr_rightview_body) 一遍又一遍,从而更频繁地刷新其内容 需要,因此显着减慢了设备。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment_swipeview_lr_mainlayout" android:layout_height="fill_parent" android:layout_width="fill_parent">
    <RelativeLayout android:id="@+id/fragment_swipeview_lr_leftview"
        android:layout_height="fill_parent" android:layout_width="wrap_content"
        android:layout_marginRight="5dip">
        <RelativeLayout android:layout_height="wrap_content"
            android:id="@+id/fragment_swipeview_lr_leftview_header"
            android:layout_alignParentTop="true" android:layout_centerHorizontal="true"
            android:background="@drawable/fragment_swipeview_header_background_left"
            android:layout_width="wrap_content" android:layout_alignLeft="@+id/fragment_swipeview_lr_leftview_body"
            android:layout_alignRight="@+id/fragment_swipeview_lr_leftview_body">
            <TextView android:layout_width="wrap_content"
                android:layout_height="wrap_content" android:id="@+id/fragment_swipeview_vt_leftview_headertext"
                android:text="Left Heading" android:textAppearance="@style/style_text_swipeview_header"
                android:layout_marginLeft="10dip"></TextView>
        </RelativeLayout>
        <RelativeLayout android:background="@drawable/fragment_swipeview_body_background_left"
            android:layout_height="fill_parent" android:layout_below="@id/fragment_swipeview_lr_leftview_header"
            android:layout_width="wrap_content" android:id="@+id/fragment_swipeview_lr_leftview_body"></RelativeLayout>
    </RelativeLayout>
    <RelativeLayout android:layout_height="fill_parent"
        android:layout_toRightOf="@+id/fragment_swipeview_lr_leftview"
        android:layout_width="wrap_content" android:id="@+id/fragment_swipeview_lr_rightview">
        <RelativeLayout android:layout_height="wrap_content"
            android:layout_width="wrap_content" android:id="@+id/fragment_swipeview_lr_header_body_wrapper"
            android:layout_toRightOf="@+id/fragment_swipeview_vi_rightview_shadow">
            <RelativeLayout android:layout_alignLeft="@+id/fragment_swipeview_lr_rightview_body"
                android:layout_alignRight="@+id/fragment_swipeview_lr_rightview_body"
                android:layout_height="wrap_content" android:layout_centerHorizontal="true"
                android:layout_width="wrap_content" android:layout_alignParentTop="true"
                android:id="@+id/fragment_swipeview_lr_rightview_header"
                android:background="@drawable/fragment_swipeview_header_background_right">
                <TextView android:text="Right Heading" android:layout_height="wrap_content"
                    android:layout_marginLeft="10dip" android:textAppearance="@style/style_text_swipeview_header"
                    android:layout_width="wrap_content" android:id="@+id/fragment_swipeview_vt_rightview_headertext"></TextView>
            </RelativeLayout>
            <RelativeLayout android:layout_height="fill_parent"
                android:layout_width="wrap_content" android:layout_below="@+id/fragment_swipeview_lr_rightview_header"
                android:id="@+id/fragment_swipeview_lr_rightview_body"
                android:background="@drawable/fragment_swipeview_body_background_right"></RelativeLayout>
        </RelativeLayout>
        <ImageView android:background="@drawable/general_shadow_left"
            android:layout_width="wrap_content" android:layout_height="fill_parent"
            android:layout_alignParentLeft="true"
            android:id="@+id/fragment_swipeview_vi_rightview_shadow"
            android:visibility="gone"></ImageView>
    </RelativeLayout>
</RelativeLayout>

使用以下代码添加ListView(尝试使用显式 LayoutParameters)。

public void addViewToLeftContentLayout(View v) { 
    leftContent.addView(v); // leftcontent == fragment_swipeview_lr_leftview_body
    fragment_swipeview_lr_leftview_body 
}

以下是表示各个测量更新的堆栈跟踪:

AdapterIngredientList.getView(int, View, ViewGroup) line: 43 
ListView(AbsListView).obtainView(int, boolean[]) line: 1970 
ListView.measureHeightOfChildren(int, int, int, int, int) line: 1228 
ListView.onMeasure(int, int) line: 1139 
ListView(View).measure(int, int) line: 10828 
RelativeLayout.measureChild(View, RelativeLayout$LayoutParams, int, 
int) line: 566 
RelativeLayout.onMeasure(int, int) line: 381 
RelativeLayout(View).measure(int, int) line: 10828 
RelativeLayout.measureChildHorizontal(View, RelativeLayout 
$LayoutParams, int, int) line: 581 
RelativeLayout.onMeasure(int, int) line: 365 
RelativeLayout(View).measure(int, int) line: 10828 
RelativeLayout.measureChildHorizontal(View, RelativeLayout 
$LayoutParams, int, int) line: 581 
RelativeLayout.onMeasure(int, int) line: 365 
RelativeLayout(View).measure(int, int) line: 10828 
RelativeLayout.measureChildHorizontal(View, RelativeLayout 
$LayoutParams, int, int) line: 581 
RelativeLayout.onMeasure(int, int) line: 365 
RelativeLayout(View).measure(int, int) line: 10828 
RelativeLayout.measureChildHorizontal(View, RelativeLayout 
$LayoutParams, int, int) line: 581 
RelativeLayout.onMeasure(int, int) line: 365 
RelativeLayout(View).measure(int, int) line: 10828 
RelativeLayout.measureChildHorizontal(View, RelativeLayout 
$LayoutParams, int, int) line: 581 
RelativeLayout.onMeasure(int, int) line: 365 
RelativeLayout(View).measure(int, int) line: 10828 
RelativeLayout.measureChildHorizontal(View, RelativeLayout 
$LayoutParams, int, int) line: 581 
RelativeLayout.onMeasure(int, int) line: 365 
RelativeLayout(View).measure(int, int) line: 10828 
RelativeLayout.measureChildHorizontal(View, RelativeLayout 
$LayoutParams, int, int) line: 581 
RelativeLayout.onMeasure(int, int) line: 365 
RelativeLayout(View).measure(int, int) line: 10828 
RelativeLayout.measureChildHorizontal(View, RelativeLayout 
$LayoutParams, int, int) line: 581 
RelativeLayout.onMeasure(int, int) line: 365 
RelativeLayout(View).measure(int, int) line: 10828 
RelativeLayout.measureChildHorizontal(View, RelativeLayout 
$LayoutParams, int, int) line: 581 
RelativeLayout.onMeasure(int, int) line: 365 
RelativeLayout(View).measure(int, int) line: 10828 
LinearLayout(ViewGroup).measureChildWithMargins(View, int, int, int, 
int) line: 4351 
LinearLayout.measureChildBeforeLayout(View, int, int, int, int, int) 
line: 1284 
LinearLayout.measureVertical(int, int) line: 613 
LinearLayout.onMeasure(int, int) line: 519 
LinearLayout(View).measure(int, int) line: 10828 
FrameLayout(ViewGroup).measureChildWithMargins(View, int, int, int, 
int) line: 4351 
FrameLayout.onMeasure(int, int) line: 267 
FrameLayout(View).measure(int, int) line: 10828 
LinearLayout.measureVertical(int, int) line: 764 
LinearLayout.onMeasure(int, int) line: 519 
LinearLayout(View).measure(int, int) line: 10828 
PhoneWindow$DecorView(ViewGroup).measureChildWithMargins(View, int, 
int, int, int) line: 4351 
PhoneWindow$DecorView(FrameLayout).onMeasure(int, int) line: 267 
PhoneWindow$DecorView.onMeasure(int, int) line: 1908 
PhoneWindow$DecorView(View).measure(int, int) line: 10828 
ViewRoot.performTraversals() line: 909 
ViewRoot.handleMessage(Message) line: 2003 
ViewRoot(Handler).dispatchMessage(Message) line: 99 
Looper.loop() line: 132 
ActivityThread.main(String[]) line: 4026 
Method.invokeNative(Object, Object[], Class, Class[], Class, int, 
boolean) line: not available [native method] 
Method.invoke(Object, Object...) line: 491 
ZygoteInit$MethodAndArgsCaller.run() line: 841 
ZygoteInit.main(String[]) line: 599 
NativeStart.main(String[]) line: not available [native method] 

任何建议/解决方法将不胜感激。 问帕特里克

1 个答案:

答案 0 :(得分:0)

我相信这种情况正在发生,因为您使用wrap_content作为布局宽度/高度。它需要一次调用才能获得测量结果,然后另一次需要实际绘制它需要的任何东西。如果可能,请尝试更改布局以使用fill_parent