Android ExpandableListView 自定义折叠图标颜色
可以通过修改 ExpandableListView 的子项布局中的 ImageView 的 src 属性来修改折叠图标的颜色。
-
首先,准备两个图标,一个是未选中状态的图标,一个是选中状态的图标。可以使用在线 SVG 编辑器(如 https://www.iconfont.cn/editor)来生成。
-
在子项布局中的 ImageView 中设置该图标,并将其 id 设置为 '@android:id/expandable_toggle_button'。
<ImageView
android:id="@android:id/expandable_toggle_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_expandable_list_group" />
- 在代码中,设置 ExpandableListView 的 GroupIndicator 为 null,这样就可以隐藏系统默认的折叠图标。
expandableListView.setGroupIndicator(null);
- 在 ExpandableListView 的 OnGroupExpandListener 和 OnGroupCollapseListener 中,根据组的展开状态,动态修改 ImageView 的 src 属性,从而实现折叠图标的颜色改变。
expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
ImageView imageView = findViewById(android.R.id.expandable_toggle_button);
imageView.setImageResource(R.drawable.ic_expandable_list_group_expanded);
}
});
expandableListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
@Override
public void onGroupCollapse(int groupPosition) {
ImageView imageView = findViewById(android.R.id.expandable_toggle_button);
imageView.setImageResource(R.drawable.ic_expandable_list_group);
}
});
其中,R.drawable.ic_expandable_list_group 和 R.drawable.ic_expandable_list_group_expanded 分别是未选中状态和选中状态的图标资源 id。可以根据需要自行替换。
原文地址: https://www.cveoy.top/t/topic/nG2D 著作权归作者所有。请勿转载和采集!