Answers:
试试这个>>>
对于所有项目
getExpandableListView().setGroupIndicator(null);
在xml中
android:groupIndicator="@null"
该android:groupIndicator
属性采用启用状态的drawable。也就是说,您可以为不同的状态设置不同的图像。
当组中没有子组时,相应的状态为“ state_empty”
请参阅以下参考链接:
对于state_empty
,您可以设置不引起混淆的其他图像,或者仅使用透明颜色不显示任何内容...
将此项目与其他项目一起添加到有状态可绘制对象中。
<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));
我已经测试了这是可行的。
根据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完成的。
另外,当默认情况下扩展组时,这种方法还会显示正确的指示符,这不适用于博客中的代码。
ViewHolder
模式。indicator
是视图持有者的变量名称。
如另一个答案所述,由于Android将未展开的列表组视为空,因此即使该组有子级也不会绘制图标。
该链接为我解决了这个问题:http : //mylifewithandroid.blogspot.com/2011/06/hiding-group-indicator-for-empty-groups.html
基本上,您必须将默认可绘制对象设置为透明,将可绘制对象作为ImageView移到组视图中,然后在适配器中切换图像。
在代码中,只需将自定义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
在XML中
android:groupIndicator="@null"
在ExpandableListAdapter
-> 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);
}
}
这可能是XML的另一种方式 android:groupIndicator="@null"
建议您我的解决方案:
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;
}
只是想改善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"/>
希望能有所帮助...记住留下赞誉
使用它对我来说是完美的工作。
<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>
您是否尝试过更改ExpandableListView
属性android:groupIndicator="@null"
?
只需为隐藏的组头创建一个高度为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>
最后,您只需在适配器类中更新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)
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);
}