android - ExpandableListView 改变 drawable state_expanded

标签 android expandablelistview android-drawable expandablelistadapter

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

我的可绘制对象看起来像这样:

<?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 是:true
  3. 州指数: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);
            }
        }
    }

    ...
}

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

关于android - ExpandableListView 改变 drawable state_expanded,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42760807/

相关文章:

java - 我需要为每个游戏级别创建一个新 Activity 吗?

Android - 从传入的短信中获取日期和时间

android - 改变文字颜色

android - Android 中具有拖放功能的可扩展列表

android - 从 BaseExpandableListAdapter 删除行后如何刷新 ListView 行?

android - 如何只用一个形状绘制多色线

android - 目录 : assets/tessdata

android - 可绘制的android使顶部边缘用不同的颜色变圆

java - Android 绘图链接失败

android - 在可扩展 ListView 中隐藏组指示器