2

カスタム ViewGroup に問題があります。3 つの子を一列にレイアウトし、Scroller を使用して中央の子までスクロールします。ユーザーのタッチ入力に基づいて、表示される子を変更し、新しいレイアウトを要求します。次に、子を新しい順序でレイアウトします。しかし、前のレイアウト実行で 1 つの子が表示された場合、子は互いに重なって表示されます。子が正しい方法でレイアウトを取得することを確認しました。古いレイアウトは削除されず、新しい子は古いレイアウトの上に描画されるだけだと思います。古いレイアウトが確実にクリアされるようにする方法はありますか?

これが私のコードです:

@Override
public boolean onTouchEvent(MotionEvent event) {
    ...
    case MotionEvent.ACTION_UP:
        if(old != current)
            this.requestLayout();
        else
            this.scrollTo(getWidth(), 0);
    ...
}

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    // show current-1, current, current+1
    int count = 0;
    for(int i = 1; i >= -1; i--) {
        // determine index of child
        // the mod does a modulo
        int index = mod(current-i, getChildCount());
        // position in row from left to right
        this.getChildAt(index).layout(count*this.getWidth(), 0, (count+1)*this.getWidth(), height);
        count++;
    }
    // scroll to middle view
    this.scrollTo(getWidth(), 0);
    ...
}
4

1 に答える 1