6

ListViewリストアイテムにバックグラウンドリソースがある場所で作業しています。できるだけ多くのオーバードローを取り除きたいです。RomainGuyのブログにあるPerformanceCaseStudyの投稿を知っていますが、リストビューを完全に最適化するのに問題があります。簡略化された例のコードは、この投稿の下部に示されています。この例は基本的に、リストビューがスローされた「新しいアクティビティ」ウィザードです。私の質問はこの例に基づいています。これは、最初の最適化されていないケースのオーバードローマーキングがある場合とない場合のスクリーンショットです。

ナイーブなケース:画面全体で2倍から3倍のオーバードロー

ページの背景が灰色(実際のプロジェクトではテクスチャ)であり、リストアイテムの背景が白(実際のプロジェクトでは9パッチ)であることを確認してください。オーバードローは劇的で、画面のどの部分も1回だけ描​​画されることはなく、リストアイテムはコンテンツの文字が表示される前に3回描画されます。

の装飾ビューの背景を取り除き、Windowオーバードローの完全なレイヤーをカットするのは簡単です。リストに画面全体を埋めるのに十分なアイテムが含まれている場合はListView、非常に良い場所にヒットしたときに背景を斧することもできます。

最適化されたケース:背景がなくなり、オーバードローはほとんどありません

残念ながら、リストの項目が画面全体よりも少ない場合、これは機能しません。背景をまったく設定しないと、リストアイテムの下には何も表示されません。リストビュー(または任意の親)に背景を設定すると、存在するすべてのリストアイテムが完全に上書きされます。

最適化されていない場合:一部のオーバードローを犠牲にして、部分的なリストを正しく処理します

ほとんどの場合、私のリストが画面からはみ出すので、それはそれほど悪いことではありませんが、不満です。オーバードローを導入せずに、最後のアイテムの後にリストビューの背景を取得する信頼できる方法はありますか?

この質問で使用されるサンプルコード:

アクティビティ:

//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" />
4

2 に答える 2

3

j__mの答えのネストされた重みの問題を回避する独自のソリューションを思いつきました。欠点は、他のListViewのカスタマイズと簡単に組み合わせることができないことです。これは、リストビューが測定中に子WRAP_CONTENTを必要としないため、リストビューの高さを含むソリューションよりも効率的である可能性があります。getView(...)

もちろん、ListView#onMeasure()実装は、親ビューを埋めるのに十分な高さがあり、とにかくgetView()同じ位置に移動すると、子の測定を停止するのに十分賢いonDraw()ので、効率の議論は議論の余地があるように見えます。それにもかかわらず、Romain Guyは、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;
    }
}
于 2013-03-26T00:31:23.993 に答える
2

ListViewをLinearLayoutに配置し、ListViewの高さをWRAP_CONTENTに設定し、その下にlayout_weight="1"を指定して別のビューを配置します。そのビューにあなたの背景を置きます。

于 2013-03-25T22:23:51.773 に答える