3

アイテムが展開されたときに別のビューでカスタマイズしたい ExpandableListView があります。問題は、state_expanded 属性に割り当てた画像が表示されないことです。状態の他のほとんどすべての属性を試しましたが、何かを行うのはstate_pressedだけで、状態が押されたときに別の画像を簡単に表示します。アイテムが展開されたときに list_item_expanded ドローアブルを表示したままにするにはどうすればよいですか? アイデアや助けをいただければ幸いです。

これが私のセレクターです。

<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:drawable="@drawable/list_item_expanded" android:state_expanded="true"></item>
  <item android:drawable="@drawable/list_item"></item>
</selector>

これが私の ExpandableListView のスタイルを設定する場所です

<style name="customExpandableList" parent="@android:style/Widget.ExpandableListView">
    <item name="android:groupIndicator">@null</item>
    <item name="android:dividerHeight">5dp</item>
    <item name="android:divider">@android:color/transparent</item>
    <item name="android:listSelector">@android:color/transparent</item>
    <item name="android:cacheColorHint">@android:color/transparent</item>
    <item name="android:childDivider">@android:color/transparent</item>
</style>

次のように、アダプターでセレクターをドローアブルとして設定します。

@Override
public View getGroupView(int groupPosition, boolean isExpanded, View view, ViewGroup parent) {
    if (view == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.trans_sum_row, null);
    }
    view.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.list_item_selector));
}
4

2 に答える 2

1

私がやろうとしていたことをする方法はないようです。このandroid:state_expanded属性は、グループ インジケーターにのみ適用され、ExpandableListView 自体には適用されません。私がやったことは、ELV用に1つのセレクターを作成し、そのようにグループインジケーター用に別のセレクターを作成することでした

<style name="customExpandableList" parent="@android:style/Widget.ExpandableListView">
  <item name="android:groupIndicator">@drawable/custom_group_selector</item>
  <item name="android:background">@drawable/custom_elv_selector</item>
  <item name="android:dividerHeight">5dp</item>
  <item name="android:divider">@android:color/transparent</item>
  <item name="android:listSelector">@android:color/transparent</item>
  <item name="android:cacheColorHint">@android:color/transparent</item>
  <item name="android:childDivider">@android:color/transparent</item>
</style>

その後、ここで SO ソリューションを使用して、希望する場所にグループ インジケーターを配置することで、希望するのと同じ効果を得ることができました。

グループインジケーターをこのように配置することは、一部の電話で異なる結果が見られるため、私にとって最良の解決策であるとは証明されていませんが、それ自体は別の投稿です.

于 2013-11-01T19:02:58.053 に答える
0

同じ問題が発生したため、グループの背景をその子に展開するなど、state_expanded フラグ (およびオプションでアイテムの state_last フラグ) に期待したことを正確に実行する信頼できるものが必要でした。

私はこのようなことをして、すべての ExpandableListView でそれを再利用できるようにしました:

アイデアは、このアダプター クラスから派生し、それが定義する 2 つの抽象メソッドを (オリジナルの代わりに) 実装することです。次に、状態ごとに 4 つの背景を作成する必要があります (私は個人的に形状を使用しましたが、ここでも 9 パッチが非常にうまくいくと確信しています)。

group_padding は、グループ ビューが左側のインジケーターの上にないことを確認するために使用されます。

public abstract class ccc71_expandable_list_adapter extends BaseExpandableListAdapter
{
protected Context context;
private int group_padding;

protected abstract View getChildView(int groupPosition, int childPosition, View convertView);

protected abstract View getGroupView(int groupPosition, View convertView, View parent);

public ccc71_expandable_list_adapter(Context ctx)
{
    super();

    context = ctx.getApplicationContext();

    TypedArray ta = context.getTheme().obtainStyledAttributes(new int[] { android.R.attr.expandableListPreferredItemPaddingLeft });

    group_padding = (int) ta.getDimension(0, 10f);

    ta.recycle();
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent)
{
    View v = getGroupView(groupPosition, convertView, parent);

    if (isExpanded)
    {
        v.setBackgroundResource(R.drawable.drop_shadow_expanded);
    }
    else
    {
        v.setBackgroundResource(R.drawable.drop_shadow_collapsed);
    }
    v.setPadding(group_padding, v.getPaddingTop(), v.getPaddingRight(), v.getPaddingBottom());

    return v;
}

@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent)
{
    View v = getChildView(groupPosition, childPosition, convertView);

    if (isLastChild)
    {
        v.setBackgroundResource(R.drawable.drop_shadow_child_last);
    }
    else
    {
        v.setBackgroundResource(R.drawable.drop_shadow_childs);
    }

    return v;
}
}
于 2015-10-03T18:28:35.883 に答える