在ExpandableListView
中,有没有办法隐藏没有孩子的群组指标?
答案 0 :(得分:101)
试试这个>>>
所有项目
getExpandableListView().setGroupIndicator(null);
在xml中
android:groupIndicator="@null"
答案 1 :(得分:88)
android:groupIndicator
属性采用启用状态的drawable。也就是说,您可以为不同的状态设置不同的图像。
当小组没有孩子时,相应的状态为'state_empty'
请参阅以下参考链接:
对于state_empty
,你可以设置一个不混淆的不同图像,或者只是使用透明色来显示任何内容......
将此项目与其他人一起添加到有状态的drawable中....
<item android:state_empty="true" android:drawable="@android:color/transparent"/>
所以,你的州名单可以是这样的:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_empty="true" android:drawable="@android:color/transparent"/>
<item android:state_expanded="true" android:drawable="@drawable/my_icon_max" />
<item android:drawable="@drawable/my_icon_min" />
</selector>
如果您使用的是ExpandableListActivity,可以在onCreate中设置groupindicator,如下所示:
getExpandableListView().setGroupIndicator(getResources().getDrawable(R.drawable.my_group_statelist));
我已经对此进行了测试。
答案 2 :(得分:38)
根据StrayPointer的回答和博客中的代码,您可以进一步简化代码:
在你的xml中添加下面的ExpandableListView:
android:groupIndicator="@android:color/transparent"
然后在适配器中执行以下操作:
@Override
protected void bindGroupView(View view, Context paramContext, Cursor cursor, boolean paramBoolean){
**...**
if ( getChildrenCount( groupPosition ) == 0 ) {
indicator.setVisibility( View.INVISIBLE );
} else {
indicator.setVisibility( View.VISIBLE );
indicator.setImageResource( isExpanded ? R.drawable.list_group_expanded : R.drawable.list_group_closed );
}
}
通过使用setImageResource方法,您可以使用单行完成所有操作。 您的适配器中不需要三个Integer数组。对于展开和折叠状态,您也不需要XML选择器。一切都是通过Java完成的。
另外,默认情况下,当某个群组展开时,此方法也会显示正确的指示符,而这与博客中的代码无关。
答案 3 :(得分:25)
正如其他答案所述,由于Android将未展开的列表组视为空,即使该组有子项,也不会绘制图标。
此链接为我解决了问题: http://mylifewithandroid.blogspot.com/2011/06/hiding-group-indicator-for-empty-groups.html
基本上你必须将默认的drawable设置为透明,将drawable作为ImageView移动到组视图中并切换适配器中的图像。
答案 4 :(得分:11)
在您的代码中,只需将自定义xml用于组列表,然后将 ImageView 用于 GroupIndicator 。
在 ExpandableListAdapter
中添加以下数组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
};
也可以在 ExpandableListAdapter的方法中添加如下内容
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent)
{
if (convertView == null)
{
LayoutInflater infalInflater = (LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.row_group_list, null);
}
//Image view which you put in row_group_list.xml
View ind = convertView.findViewById(R.id.iv_navigation);
if (ind != null)
{
ImageView indicator = (ImageView) ind;
if (getChildrenCount(groupPosition) == 0)
{
indicator.setVisibility(View.INVISIBLE);
}
else
{
indicator.setVisibility(View.VISIBLE);
int stateSetIndex = (isExpanded ? 1 : 0);
Drawable drawable = indicator.getDrawable();
drawable.setState(GROUP_STATE_SETS[stateSetIndex]);
}
}
return convertView;
}
<强>参考:强> http://mylifewithandroid.blogspot.in/2011/06/hiding-group-indicator-for-empty-groups.html
答案 5 :(得分:7)
这可能是另一种来自XML的方式,设置为android:groupIndicator="@null"
答案 6 :(得分:3)
建议你我的解决方案:
1)清除默认的groupIndicator:
<ExpandableListView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="240dp"
android:layout_gravity="start"
android:background="#cccc"
android:groupIndicator="@android:color/transparent"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
/>
2)在ExpandableAdapter中:
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = new TextView(context);
}
((TextView) convertView).setText(groupItem.get(groupPosition));
((TextView) convertView).setHeight(groupHeight);
((TextView) convertView).setTextSize(groupTextSize);
//create groupIndicator using TextView drawable
if (getChildrenCount(groupPosition)>0) {
Drawable zzz ;
if (isExpanded) {
zzz = context.getResources().getDrawable(R.drawable.arrowup);
} else {
zzz = context.getResources().getDrawable(R.drawable.arrowdown);
}
zzz.setBounds(0, 0, groupHeight, groupHeight);
((TextView) convertView).setCompoundDrawables(null, null,zzz, null);
}
convertView.setTag(groupItem.get(groupPosition));
return convertView;
}
答案 7 :(得分:2)
在XML
中android:groupIndicator="@null"
在ExpandableListAdapter
- &gt; getGroupView
复制以下代码
if (this.mListDataChild.get(this.mListDataHeader.get(groupPosition)).size() > 0){
if (isExpanded) {
arrowicon.setImageResource(R.drawable.group_up);
} else {
arrowicon.setImageResource(R.drawable.group_down);
}
}
答案 8 :(得分:0)
使用它对我来说非常有用。
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/group_indicator_expanded" android:state_empty="false" android:state_expanded="true"/>
<item android:drawable="@drawable/group_indicator" android:state_empty="true"/>
<item android:drawable="@drawable/group_indicator"/>
</selector>
答案 9 :(得分:0)
简单地说,您为隐藏的组头创建了一个高度为0的新xml布局。 例如,它是'group_list_item_empty.xml'
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="0dp">
</RelativeLayout>
然后您的正常组头布局为'your_group_list_item_file.xml'
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="48dp"
android:orientation="horizontal">
...your xml layout define...
</LinearLayout>
最后,您只需更新Adapter Class中的getGroupView方法:
public class MyExpandableListAdapter extends BaseExpandableListAdapter{
//Your code here ...
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup viewGroup) {
if (Your condition to hide the group header){
if (convertView == null || convertView instanceof LinearLayout) {
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.group_list_item_empty, null);
}
return convertView;
}else{
if (convertView == null || convertView instanceof RelativeLayout) {
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.your_group_list_item_file, null);
}
//Your code here ...
return convertView;
}
}
}
重要:布局文件的根标签(隐藏和正常)必须不同(如上例所示,LinearLayout和RelativeLayout)
答案 10 :(得分:0)
您是否尝试更改+
的属性ExpandableListView
?
答案 11 :(得分:0)
只想改进Mihir Trivedi的答案。您可以将它放在MyExpandableListAdapter类
中的getGroupView()中 View ind = convertView.findViewById(R.id.group_indicator);
View ind2 = convertView.findViewById(R.id.group_indicator2);
if (ind != null)
{
ImageView indicator = (ImageView) ind;
if (getChildrenCount(groupPosition) == 0)
{
indicator.setVisibility(View.INVISIBLE);
}
else
{
indicator.setVisibility(View.VISIBLE);
int stateSetIndex = (isExpanded ? 1 : 0);
/*toggles down button to change upwards when list has expanded*/
if(stateSetIndex == 1){
ind.setVisibility(View.INVISIBLE);
ind2.setVisibility(View.VISIBLE);
Drawable drawable = indicator.getDrawable();
drawable.setState(GROUP_STATE_SETS[stateSetIndex]);
}
else if(stateSetIndex == 0){
ind.setVisibility(View.VISIBLE);
ind2.setVisibility(View.INVISIBLE);
Drawable drawable = indicator.getDrawable();
drawable.setState(GROUP_STATE_SETS[stateSetIndex]);
}
}
}
...至于布局视图,这就是我的group_items.xml似乎是
的方式<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/group_heading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="20dp"
android:paddingTop="16dp"
android:paddingBottom="16dp"
android:textSize="15sp"
android:textStyle="bold"/>
<ImageView
android:id="@+id/group_indicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/arrow_down_float"
android:layout_alignParentRight="true"
android:paddingRight="20dp"
android:paddingTop="20dp"/>
<ImageView
android:id="@+id/group_indicator2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/arrow_up_float"
android:layout_alignParentRight="true"
android:visibility="gone"
android:paddingRight="20dp"
android:paddingTop="20dp"/>
希望有所帮助......记得留下一个upvote
答案 12 :(得分:0)
此处发布的答案将使您的小组指示器消失,即使小组有孩子也是如此。
要解决此问题,请覆盖组指示器。
首先,将您的组指示器设置为空:
ExpandableListView.setGroupIndicator(null);
然后,转到包含标题的 xml,并添加带有可绘制对象的 ImageView。
之后,转到您的 ExpandableListAdapter.java,并在“getGroupView”部分中,执行以下操作:
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_group, null);
}
TextView lblListHeader = convertView
.findViewById(R.id.lblListHeader); // this is your header
ImageView groupIndicator = convertView.findViewById(R.id.group_indicator); // this is the ImageView included in your xml to override the indicator
if (getChildrenCount(groupPosition) == 0) {
convertView.setVisibility(View.GONE);
lblListHeader.setVisibility(View.GONE);
convertView.setPadding(0,0,0,0); //try not to include padding on your xml, and instead define the padding here.
} else {
convertView.setVisibility(View.VISIBLE);
convertView.setPadding(8,8,8,0);
lblListHeader.setVisibility(View.VISIBLE);
lblListHeader.setTypeface(font);
lblListHeader.setText(headerTitle);
if (isExpanded) { //this is the part where we define our group indicator based on the expanded status of the adapter
groupIndicator.setImageResource(R.drawable.group_indicator_expanded);
} else {
groupIndicator.setImageResource(R.drawable.group_indicator_empty);
}
}
return convertView;
}
答案 13 :(得分:-2)
convertView.setVisibility(View.GONE)应该可以解决问题。
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
DistanceHolder holder;
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.list_search_distance_header, parent, false);
if (getChildrenCount(groupPosition)==0) {
convertView.setVisibility(View.GONE);
}