ExpandableListView ——无子组的隐藏指示符

ExpandableListView中,是否有方法隐藏没有子组的组指示符?

97370 次浏览

android:groupIndicator属性采用一个启用状态的可绘制属性。也就是说,您可以为不同的状态设置不同的图像。

当组没有子级时,对应的状态是“ state _ void”

查看以下参考链接:

这个和这个 href = “ http://developer.android.com/reference/android/R.attr.html # state _ void”rel = “ norefrer”> this

For state_empty, you can set a different image which is not confusing, or, simply use transparent color to display nothing...

将此项与其他项一起添加到您的有状态绘图中... ..。

<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 中按照以下方式设置 group 指示符:

getExpandableListView().setGroupIndicator(getResources().getDrawable(R.drawable.my_group_statelist));

我已经测试过了。

正如在另一个答案中提到的,因为 Android 将未展开的列表组视为空,所以即使该组有子组,也不会绘制图标。

这个链接为我解决了问题: Http://mylifewithandroid.blogspot.com/2011/06/hiding-group-indicator-for-empty-groups.html

Basically you have to set the default drawable as transparent, move the drawable into your group view as an ImageView and toggle the image in your adapter.

根据 StrayPointer 的回答和博客中的代码,您可以更加简化代码:

在 xml 中,将以下内容添加到 ExpandableListView:

android:groupIndicator="@android:color/transparent"

Then in the Adapter you do the following:

@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 完成的。

另外,这种方法还可以显示默认情况下展开组时的正确指示符,这些指示符对博客中的代码不起作用。

试试这个

for all items

 getExpandableListView().setGroupIndicator(null);

在 xml 中

android:groupIndicator="@null"

用这个,对我来说非常有效。

<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>

在您的代码中,只需将自定义 xml 用于组列表,并将 图片浏览用于 群组指标

并在 可扩展列表适配器中添加以下数组

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's方法中添加与下面相同的内容

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

建议你我的解决方案:

1)清除默认群组指示器:

<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;
}

Simply, you create a new xml layout with height=0 for the hidden group header. 例如,它是‘ 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)

这可能是从 XML 的另一种方式,设置 android:groupIndicator="@null"

参考链接: https://stackoverflow.com/a/5853520/2624806

您是否尝试过更改 ExpandableListView的属性 android:groupIndicator="@null"

convertView.setVisibility(View.GONE) should do the trick.

@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);
}

只是想改进 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]);
}
}
}

...and as for the layout view, this is how my group_items.xml appears to be

<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"/>

希望能有所帮助... 记得投赞成票

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);
}
}

这里张贴的答案会让你的群组指示消失,即使一个群组有孩子。

要解决此问题,请重写组指示符。

首先,将组指示器设置为 null:

    ExpandableListView.setGroupIndicator(null);

然后,转到包含头部的 xml,并添加一个带有绘图工具的 ImageView。

After that, go to your ExpandableListAdapter.java, and in the "getGroupView" section, do the following:

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;
}