可扩展列表视图中自定义分组展开图标

5

enter image description here

我试图为ExpandableListView的组展开和折叠状态使用自定义图标。但是这似乎不起作用。即使输出消息按顺序工作,图标也不会改变。

    explistView.setOnGroupClickListener(new OnGroupClickListener()
    {
        @Override
        public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id)
        {
            groupIndicator = (ImageView) findViewById(R.id.group_indicator);
            if (parent.isGroupExpanded(groupPosition))
            {
                System.out.println("1");
                parent.collapseGroup(groupPosition);
                System.out.println("2");
                groupIndicator.setImageResource(R.drawable.expand_icon_35x35);
                System.out.println("3");
                adapter.notifyDataSetChanged();
            } 
            else
            {
                System.out.println("4");
                parent.expandGroup(groupPosition);
                System.out.println("5");
                groupIndicator.setImageResource(R.drawable.collapse_icon_35x35);
                System.out.println("6");
                adapter.notifyDataSetChanged();
            }
            return true;
        }
    });

你设置了 setGroupIndicator(null) 吗? - Anjali
2个回答

0
你可以参考这个答案
你可以通过以下方式更改图标:
<ExpandableListView android:id="@+id/list"
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent"
       android:layout_marginTop="10dp"
       android:groupIndicator="@drawable/settings_selector"
   />

设置选择器为:

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

我已经使用过这个方法,它可以正常工作,但是图标会被拉伸。因此,我转而采用了上述方法。 - Veronika Gilbert
@VeronikaGilbert 请尝试将您的图标调整为32*32 dp。 - Anirudh Sharma
@VeronikaGilbert,请上传一张图片。这可能是图标切片的问题,与代码无关。 - Anirudh Sharma
我已经添加了图片,其中图标会自动扩展以填充空间。 - Veronika Gilbert
你能建议我需要使用的图像尺寸吗? - Veronika Gilbert
@VeronikaGilbert 我看问题在于可扩展列表视图组指示器会扩展以适应视图的大小。因此,您可以使用9个补丁图像。我在谷歌上搜索并找到了以下被接受的答案:https://dev59.com/TGsy5IYBdhLWcg3w8Slc和https://dev59.com/TGsy5IYBdhLWcg3w8Slc。请检查这些答案,因为它们可能有所帮助。 - Anirudh Sharma

0
在我的情况下,android:state_empty="true" 不起作用。因此,我将其更改为 android:state_expanded="false"
<ExpandableListView 
       android:id="@+id/expl"
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent"
       android:groupIndicator="@drawable/settings_selector"/>

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

希望它有所帮助。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接