0

そこで、「マルチスライディングドロワー」ウィジェットを作成しました。これは、最大4つの「ドロワー」を許可することを除けばSlidingdrawerと同じように機能します。ただし、この「n-drawers」を作成したいのですが、問題はxmlのパラメーターにあります。現在、私は以下を介してハンドル/コンテンツを渡します:

ns:handle1="@+id/slideHandleButton1"
ns:content1="@+id/contentLayout1"
ns:handle2="@+id/slideHandleButton2"
ns:content2="@+id/contentLayout2"
ns:handle3="@+id/slideHandleButton3"
ns:content3="@+id/contentLayout3"       

しかし、明らかにここにはある程度の冗長性があります。当初は、「getChild(i)」を使用して子を循環させて内部的に追加できると思っていましたが、getChildメソッドは、xmlで追加された順序ではなく、視覚的な順序で子を返すことを理解しています。だから私が今やりたいことは次のようなものです:

ns:handles="@id/contentLayout1,@id/contentLayout2,@id/contentLayout13"

これにより、任意の数の引き出しが可能になります。これは可能ですか?または、この問題に対する別の良い解決策はありますか?

4

1 に答える 1

1

それぞれが配列への参照をとるパラメーター(たとえば、handlesおよび)を持つようにウィジェットを定義できます。contents標準表記を使用して配列を定義できます。

res / values / arrays.xml

<array name="my_widget_layouts">
    <item>@layout/contentLayout1</item>
    <item>@layout/contentLayout2</item>
    . . .
</array>

<array name="my_widget_buttons">
    <item>@+id/slideHandleButton1</item>
    <item>@+id/slideHandleButton2</item>
    . . .
</array>

いくつかのレイアウト内:

<com.example.MyWidget
    ns:contents="@array/my_widget_layouts"
    ns:handles="@array/my_widget/buttons"
    . . .
    />

次に、ウィジェットを作成するときのJavaコードで次のようにします。

MyWidget(Context context, AttributeSet attrs) {
    Resources res = getResources();
    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyWidget);
    // Get the ID of the array containing the content layout IDs for this widget
    int contentsId = a.getResourceId(R.styleable.MyWidget_contents, 0);
    if (contentsId > 0) {
        // array of layouts specified
        TypedArray ta = res.obtainTypedArray(contentsId);
        int n = ca.length();
        mContentLayoutIds = new int[n];
        for (int i = 0; i < n; ++i) {
            mContentLayoutIds[i] = ta.getResourceId(i, 0);
            // error check: should never be 0
        }
        ta.recycle();
    }
    // similar code to retrieve the button IDs
    if (mContentLayoutIds.length != mHandleIds.length) {
        // content layout and button arrays not same length: throw an exception
    }
    . . .
    a.recycle();
    . . .
}
于 2012-07-08T22:57:57.917 に答える