1

ExpandableListAdapter.getChildView で発生している問題をデバッグしようとしています。

リスト項目の背景に、グラデーションと半径 1 のコーナーを含む形状の Drawable を定義しました。特別なことは何もありません。

次に、私のアダプタ コードでは、getChildView 内に次のスニペットがあります。

GradientDrawable background = (GradientDrawable) convertView.getBackground();
float topRadius = 0;
float bottomRadius = 0;

// Make the corner radius obvious for debugging             
if (childPosition == 0)
    topRadius = 14;
if (childPosition == (mValues.size() - 1))
    bottomRadius = 14;

background.setCornerRadii(new float [] { topRadius, topRadius, 
                                         topRadius, topRadius,  
                                         bottomRadius, bottomRadius, 
                                         bottomRadius, bottomRadius});
convertView.setBackgroundDrawable(background);

ここでの試みは、最初のリスト項目の上部と最後のリスト項目の下部を丸めることです。デバッグを介して、必要なアイテムに必要な値を設定しているように見えます。

しかし、私が抱えている問題は、隅の半径がすべてのリスト項目に対して下の項目であるかのように設定されていることです。

少し余談ですが、少なくともデバッグ目的で、GradientDrawable の角の半径を取得する方法はありますか?

ありがとう、
wTs

4

2 に答える 2

16

setCornerRadii() を呼び出す代わりに、mutate().setCornerRadii() を呼び出すと、問題が解決するはずです (完全な説明はhttp://www.curious-creature.org/2009/05/02/drawable-mutations/にあります) 。

編集 (by Wonko)
これはほぼ正しいです (提供されたリンクは基本的に理由を説明しています)。唯一の変更点は、mutate() が setCornerRadii() メソッドを持たない Drawable を返すことです。代わりに、変更されたオブジェクトを GradientDrawable にキャストする必要があり、それは魅力的に機能します。

((GradientDrawable) background.mutate()).setCornerRadii(
    new float [] { topRadius, topRadius, topRadius, topRadius,  
                   bottomRadius, bottomRadius, bottomRadius, bottomRadius});
于 2010-12-03T17:32:53.443 に答える
2

編集:これはOPの問題の修正ではないことに気付きました。

Mathias のコメントに加えて、これを修正するには、最初に半径を 1 に設定してから、個々の角の半径を設定します。

background.setCornerRadius(1.0f);
background.setCornerRadii(new float [] { topRadius, topRadius,  
                                         topRadius, topRadius,   
                                         bottomRadius, bottomRadius,  
                                         bottomRadius, bottomRadius}); 

これで問題が解決するかどうかお知らせください。コーナー半径を取得する限り、そうする方法はわかりません...

于 2010-12-03T17:21:19.867 に答える