在一个ExpandableListView
中,有没有办法隐藏没有孩子的组的组指示符?
14 回答
试试这个>>>
对于所有项目
getExpandableListView().setGroupIndicator(null);
在xml中
android:groupIndicator="@null"
该android:groupIndicator
属性采用状态启用的可绘制对象。也就是说,您可以为不同的状态设置不同的图像。
当组没有孩子时,对应的状态是'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 方法,您可以用一条线完成所有操作。您不需要适配器中的三个整数数组。您也不需要用于展开和折叠状态的 XML 选择器。一切都是通过Java完成的。
此外,这种方法还可以在默认情况下扩展组时显示正确的指示符,哪些内容不适用于博客中的代码。
如另一个答案中所述,由于Android将未扩展的列表组视为空,因此即使该组有子项,也不会绘制图标。
这个链接为我解决了这个问题:http: //mylifewithandroid.blogspot.com/2011/06/hiding-group-indicator-for-empty-groups.html
基本上,您必须将默认可绘制对象设置为透明,将可绘制对象作为 ImageView 移动到您的组视图中,并在适配器中切换图像。
在 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 用于组列表,然后将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"
建议你我的解决方案:
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) 在可扩展适配器中:
@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
's 的属性android:groupIndicator="@null"
?
简单地说,您为隐藏的组标题创建一个 height=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 )
即使小组有孩子,此处发布的答案也会使您的小组指示器消失。
要解决此问题,请覆盖组指示器。
首先,将您的组指示符设置为空:
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;
}
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);
}