【问题标题】:ExpandableListView change drawable state_expandedExpandableListView 更改可绘制状态_expanded
【发布时间】:2017-08-03 07:15:24
【问题描述】:

我的ExpandableListAdapter 有问题。我使用自定义图像作为indicator,因为我想将其放置在菜单项的右侧而不是左侧。我还需要在某些项目上隐藏indicator

我的drawable看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:state_expanded="true"
    android:drawable="@drawable/arrow_up" />
<item
    android:drawable="@drawable/arrow_down" />
</selector>

所以我使用了Androidstate_expanded

我的DrawerLayout 里面有这个LinearLayout

<LinearLayout
    android:id="@+id/drawer_linear_layout"
    android:layout_width="@dimen/menu_width"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:orientation="vertical">

    <ExpandableListView
        android:id="@+id/navigationmenu"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:layout_marginTop="@dimen/nav_header_height"
        android:childDivider="@android:color/transparent"
        android:headerDividersEnabled="true"
        android:groupIndicator="@android:color/transparent"
        android:background="@drawable/border_shadow">
    </ExpandableListView>

</LinearLayout>

我手动隐藏了正常的groupIndicator,以便稍后在代码中添加自定义Drawable

我的ExpandableListAdapter 似乎是正确的:

public class ExpandableListAdapter extends BaseExpandableListAdapter {
    ...

    private static final int[] EMPTY_STATE_SET = {};
    private static final int[] GROUP_EXPANDED_STATE_SET = { android.R.attr.state_expanded };
    private static final int[][] GROUP_STATE_SETS = {
        EMPTY_STATE_SET, // 0
        GROUP_EXPANDED_STATE_SET //1
    };

    ...

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        MOMenuItem headerTitle = (MOMenuItem) getGroup(groupPosition);

        LayoutInflater infalInflater = (LayoutInflater) this.mContext
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        LinearLayout linLayout = (LinearLayout) infalInflater.inflate(R.layout.list_header, parent, false);
        convertView = linLayout;

        View indicator = convertView.findViewById(R.id.indicator_image);

        if (groupPosition == 1) {
            Log.d("GetGroupView", "Called!");
        }

        if (indicator != null) {
            ImageView indicatorImage = (ImageView) indicator;
            if (getChildrenCount(groupPosition) == 0) {
                indicatorImage.setVisibility(View.INVISIBLE);
            } else {
                if (groupPosition == 1) {
                    Log.d("IsExpanded is", "" + isExpanded);
                }
                indicatorImage.setVisibility(View.VISIBLE);
                int stateSetIndex = (isExpanded ? 1 : 0);
                if (groupPosition == 1) {
                    Log.d("State Index", "" + stateSetIndex);
                }
                Drawable drawable = indicatorImage.getDrawable();
                drawable.setState(GROUP_STATE_SETS[stateSetIndex]);
            }
        }

        ...

        return convertView;
    }
}

问题是indicator 没有得到更新。但是,当打开索引 1 处的项目时,Log-Statements 的输出是:

  1. GetGroupView:已调用!
  2. IsExpanded 为:真
  3. 状态指数:1

indicator 保持不变。

【问题讨论】:

    标签: android expandablelistview android-drawable expandablelistadapter


    【解决方案1】:

    经过长时间的研究和反复试验,我没有找到使其适用于状态的方法。但是,我使用图像本身如下:

    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        MOMenuItem headerTitle = (MOMenuItem) getGroup(groupPosition);
    
        LayoutInflater infalInflater = (LayoutInflater) this.mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        LinearLayout linLayout = (LinearLayout) infalInflater.inflate(R.layout.list_header, parent, false);
        convertView = linLayout;
    
        View indicator = convertView.findViewById(R.id.indicator_image);
    
        if (indicator != null) {
            ImageView indicatorImage = (ImageView) indicator;
            if (getChildrenCount(groupPosition) == 0) {
                indicatorImage.setVisibility(View.INVISIBLE);
            } else {
                indicatorImage.setVisibility(View.VISIBLE);
    
                if (isExpanded) {
                    indicatorImage.setImageResource(R.drawable.arrow_up);
                } else {
                    indicatorImage.setImageResource(R.drawable.arrow_down);
                }
            }
        }
    
        ...
    }
    

    遗憾的是,带有可绘制和状态的更清洁的解决方案不起作用。我希望这会帮助其他人偶然发现这个问题。

    【讨论】:

    • @Bikram 当然,您需要在项目中拥有具有该 ID 的图像。我在 2017 年遇到过。但 Android 发生了很大变化,所以我想说我的答案可能已经过时了。
    【解决方案2】:

    按照这个https://stackoverflow.com/a/57369511/6117565 并在您的模型中以编程方式将图标添加到特定列表项。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-20
      • 1970-01-01
      • 2013-11-18
      • 1970-01-01
      相关资源
      最近更新 更多