[ACCEPTED]-ExpandableListView - hide indicator for groups with no children-expandablelistview
Try this >>>
for all items
getExpandableListView().setGroupIndicator(null);
In xml
android:groupIndicator="@null"
0
The android:groupIndicator
property takes a state enabled drawable. That 12 is, you can set different image for different 11 states.
When the group has no children, the 10 corresponding state is 'state_empty'
See 9 these reference links:
For state_empty
, you can 8 set a different image which is not confusing, or, simply 7 use transparent color to display nothing...
Add 6 this item in your stateful drawable along 5 with others....
<item android:state_empty="true" android:drawable="@android:color/transparent"/>
So, your statelist 4 can be like this:
<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>
In case you are using an 3 ExpandableListActivity, you can set the 2 groupindicator in onCreate as follows:
getExpandableListView().setGroupIndicator(getResources().getDrawable(R.drawable.my_group_statelist));
I 1 have tested this to be working.
Based on StrayPointer's answer and the code 12 from the blog, you can simplify the code 11 even more:
In your xml add the folowing to 10 ExpandableListView:
android:groupIndicator="@android:color/transparent"
Then in the Adapter you 9 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 );
}
}
By using the setImageResource 8 method you get it all done with a one-liner. You 7 do not need the three Integer arrays in 6 your adapter. You also do not need an XML 5 selector for state expanded and collapsed. All 4 is done via Java.
Plus, this approach also 3 displays the correct indicator when a group 2 is expanded by default what does not work 1 with the code from the blog.
As mentioned in a different answer, since 7 Android treats an un-expanded list group 6 as empty, the icon is not drawn even if 5 the group has children.
This link solved 4 the problem for me: http://mylifewithandroid.blogspot.com/2011/06/hiding-group-indicator-for-empty-groups.html
Basically you have to 3 set the default drawable as transparent, move 2 the drawable into your group view as an 1 ImageView and toggle the image in your adapter.
In XML
android:groupIndicator="@null"
In ExpandableListAdapter
-- > getGroupView
copy following code
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);
}
}
0
In your code just use the custom xml for 3 group list and in that put the ImageView for GroupIndicator.
And 2 Add below arrays in your 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
};
also in ExpandableListAdapter's method 1 add same things as below
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;
}
Reference: http://mylifewithandroid.blogspot.in/2011/06/hiding-group-indicator-for-empty-groups.html
this could be another way from XML, set 1 android:groupIndicator="@null"
Reference Link: https://stackoverflow.com/a/5853520/2624806
suggest you my solution:
1)Clear default 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) in 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;
}
Just wanted to improve on Mihir Trivedi's 5 answer. You can place this within getGroupView() that's 4 inside MyExpandableListAdapter class
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 3 as for the layout view, this is how my group_items.xml 2 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"/>
Hope that helps...remember 1 to leave an upvote
Use this it is perfectly working for me.
<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>
0
Have you tried to change ExpandableListView
's attribute android:groupIndicator="@null"
?
0
Simply, you create a new xml layout with 8 height=0 for the hidden group header. For 7 example, it's '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>
Then 6 your normal group header layout is '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>
Finally, you 5 just update the getGroupView method in your 4 Adapter Class:
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;
}
}
}
IMPORTANT: The root tag of the layout 3 files (hidden and normal) must be different 2 (as above example, LinearLayout and RelativeLayout 1 )
The answers posted around here will make 8 your group indicator gone even when a group 7 has children.
To solve this, override the 6 group indicator.
First, set your group indicator 5 as null:
ExpandableListView.setGroupIndicator(null);
Then, go to your xml that contains 4 the header, and add an ImageView with your 3 drawable.
After that, go to your ExpandableListAdapter.java, and 2 in the "getGroupView" section, do 1 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;
}
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.