安卓默认分组指示器ExpandableListView

4
我正在使用可扩展列表视图,用户可以选择数据。
用户可以选择组或子项。为了区分这两者,我有一个带有两个选项的单选按钮组:
- 选择子项:正常的可扩展列表(可以展开组并选择子项) - 选择组:所有组都折叠起来,用户无法展开组
我需要做的是,在第二种情况下隐藏组指示器,然后在选择第一种选项时恢复它。
以下是我的代码:
rgLink.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            if(checkedId == R.id.rb_link_subject){ //Here is the child mode
                mLinkType = LINK_SUBJECT;
                //elvSubject.setGroupIndicator(/*Here I need the default group indicator*/);
            }
            else{                                  //Here is the group mode
                collapseAllChildren();
                //The line below hide the group indicator
                elvSubject.setGroupIndicator(null);
                mLinkType = LINK_CATEGORY;
            }
        }
    });

我也在使用以下代码作为组合项:

style="?android:attr/listSeparatorTextViewStyle" 

基本上,我只需要一行代码来恢复默认的组指示器。我该怎么做?
1个回答

12

您可以从当前主题中获取属性:

 //obtain expandableListViewStyle  from theme
 TypedArray expandableListViewStyle = context.getTheme().obtainStyledAttributes(new int[]{android.R.attr.expandableListViewStyle});
 //obtain attr from style
 TypedArray groupIndicator = context.getTheme().obtainStyledAttributes(expandableListViewStyle.getResourceId(0,0),new int[]{android.R.attr.groupIndicator});
 elvSubject.setGroupIndicator(groupIndicator.getDrawable(0));
 expandableListViewStyle.recycle();
 groupIndicator.recycle();

这正是我正在寻找的。有了它,我可以在需要时恢复组指示器。 - FR073N
你能否解释一下你是怎么知道群组指示器样式是0索引的?有网站或文件吗? - FR073N
2
obtainStyledAttributes 返回一个 TypedArray,其中包含主题中定义的在 attrs 中列出的值 (链接)。在这两种情况下,我们都传递了长度为1的 attrs 数组,因此结果包含1个元素,该元素具有索引0(它是数组中的索引,而不是资源ID)。 - esentsov

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