摆脱每个项目背景的ListView中的透支

时间:2013-03-25 22:20:42

标签: android listview optimization

我正在使用ListView,其中列表项具有后台资源。我想摆脱尽可能多的透支。我知道Romain Guy博客上的Performance Case Study帖子,但我无法完全优化列表视图。简化示例的代码显示在本文的底部。该示例基本上只是“新活动”向导,其中包含一个列表视图。我的问题建立在此示例的基础上。这是一个截图,有或没有过度标记初始的,未经优化的案例:

Naive case: 2x-3x overdraw for the entire screen

见证页面有灰色背景(这是我真实项目中的纹理),列表项目有白色背景(在我的实际项目中是9个补丁)。透支是戏剧性的,屏幕的任何部分都不会被绘制一次,列表项目会在显示内容信之前抽出三次。

很容易摆脱Window中的装饰视图背景并剪掉一层完整的透支。如果列表中包含足够的项目来填充整个屏幕,我可以在ListView背景中找到一个非常好的位置:

Optimized case: backgrounds gone, almost no overdraw

不幸的是,当列表中的项目少于填满整个屏幕时,这不起作用。如果我没有设置任何背景,列表项下面没有任何内容。如果我在列表视图(或任何父级)上设置背景,我会对所有存在的列表项进行完全透支:

Deoptimized case: handles partial lists correctly at the cost of some overdraw

这是较小的邪恶,但不满意,因为大多数时候我的列表将在屏幕上运行。有没有可靠的方法在最后一个项目后的列表视图中获取背景,而不引入过度绘制?

此问题中使用的示例代码:

活动:

//MainActivity.java
public class MainActivity extends Activity {
    private static final String[] DATA = { "Alpha", "Bravo", "Charlie", "Delta", "Echo", "Foxtrot", "Golf", "Hotel", "India", "Juliet" };

    @Override protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // Optimization 1: getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        ListView lv = (ListView) findViewById(android.R.id.list);
        lv.setAdapter(new ArrayAdapter<String>(this, R.layout.activity_main__list_item,
                    android.R.id.text1, data));
    }
}

布局:

<!-- layout/activity_main.xml -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <TextView
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:background="#FFDDDDDD"
        android:text="Page header information" />
    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#FFDDDDDD"
        android:divider="@null" />
</LinearLayout>

<!-- layout/activity_main__list_item.xml -->
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/text1"
    android:layout_width="match_parent" 
    android:layout_height="48dp"
    android:background="@android:color/white" />

2 个答案:

答案 0 :(得分:3)

我想出了一个我自己的解决方案,避免了j__m answer的嵌套权重问题。在缺点方面,它不能轻易与其他ListView自定义组合。对于列表视图的高度,这可能比涉及WRAP_CONTENT的解决方案更有效,因为ListView在测量期间不需要getView(...)其子项。

当然,ListView#onMeasure()实施足够智能,一旦有足够的高度来填充其父视图,并且在getView()期间到达onDraw()那些相同的位置,就会停止测量儿童,效率论证似乎没有实际意义。然而,Romain Guy是quite insistent,ListView不应该使用WRAP_CONTENT

/**
 * A list view for use with list items that have their own background drawable. Such list views
 * suffer from GPU Overdraw of the item background on top of the list view (ancestor) background.
 * <p>
 * This subclass detects when its data set contains enough elements to fill all available space
 * and start scrolling. If this is the case, it sets its own background to transparent.
 * </p>
 * <p><strong>Limitation:</strong> Header and Footer views are ignored. If the list view has few
 * enough items that it wouldn't scroll, but a header and/or footer are big enough to cause it to
 * scroll anyway, the background is not hidden and overdraw is present.
 * </p>
 * <p>Source: https://stackoverflow.com/q/15625930/49489 CC-BY-SA</p>
 */
public class BackgroundListView extends ListView {

    /** We need our own instance, because it's used as a sentinel value later on. */
    private static final Drawable TRANSPARENT = new ColorDrawable(0x00000000);

    /** If true, the next call to {@code onDraw(Canvas)} evaluates whether to show or hide the background. */
    private boolean mNeedsBackgroundCheck = true;

    /** The background to restore if the list shrinks. */
    private Drawable mOriginalBackground;

    public BackgroundListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public BackgroundListView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BackgroundListView(Context context) {
        this(context, null, 0);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        this.mOriginalBackground = this.getBackground();
    }

    @Override
    public void setBackground(Drawable background) {
        super.setBackground(background);
        Drawable newBackground = getBackground();
        if (newBackground != TRANSPARENT) {
            this.mOriginalBackground = newBackground;
        }
    }

    @Override
    public void setBackgroundResource(int resid) {
        super.setBackgroundResource(resid);
        Drawable newBackground = getBackground();
        if (newBackground != TRANSPARENT) {
            this.mOriginalBackground = newBackground;
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (mNeedsBackgroundCheck) {
            maybeHideBackground();
        }
        super.onDraw(canvas);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mNeedsBackgroundCheck = true;
    };

    @Override
    protected void handleDataChanged() {
        super.handleDataChanged();
        mNeedsBackgroundCheck = true;
    }

    private void maybeHideBackground() {
        if (isInEditMode()) {
            return;
        }
        final int maxPosition = getAdapter().getCount() - 1;
        final int firstVisiblePosition = getFirstVisiblePosition();
        final int lastVisiblePosition = getLastVisiblePosition();
        if (firstVisiblePosition > 0 || lastVisiblePosition < maxPosition) {
            setBackground(TRANSPARENT);
        } else {
            setBackground(mOriginalBackground);
        }
        mNeedsBackgroundCheck = false;
    }
}

答案 1 :(得分:2)

将ListView放在LinearLayout中,将ListView的高度设置为WRAP_CONTENT,并使用layout_weight =“1”将另一个视图放在其下面。把你的背景放在那个视图上。