Programmatically collapse a group in ExpandableListView
Try putting this in your ExpandableListAdapter
, listView
is a reference to the ExpandableListView
itself. And lastExpandedGroupPosition
is a integer member variable defined inside your ExpandableListAdapter
.
@Override
public void onGroupExpanded(int groupPosition){
//collapse the old expanded group, if not the same
//as new group to expand
if(groupPosition != lastExpandedGroupPosition){
listView.collapseGroup(lastExpandedGroupPosition);
}
super.onGroupExpanded(groupPosition);
lastExpandedGroupPosition = groupPosition;
}
Expand group row programmatically in expandable list
In adapter
private OnItemSelectedListener onItemSelectedCallback;
public interface OnItemsSelectedListener {
public void onImageSelected(int groupPos);
}
public YourAdapter(Context context) {
try {
this.onItemSelectedCallback = (OnItemSelectedListener ) context;
}
catch (ClassCastException e) {
throw new ClassCastException(context.toString() + " must implement OnItemSelectedListener ");
}
}
Add this in getView()
ImageView imageView = new ImageView();
imageView.setTag(R.id.tagGroupPosition, groupPosition);
imageView.setOnClickListener(onClickListener);
Add this in OnClickListener
OnClickListener onClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
int groupPos = ((ImageView) v).getTagId(R.id.tagGroupPosition);
onItemSelectedCallback.onImageSelected(groupPos);
}
}
Then in activity you have to implements YourAdapter.OnItemsSelectedListener the Override onImageSelected
@Override
public void onImageSelected(int groupPos){
if(expandableList.isGroupExpanded(groupPos)){
expandableList.collapseGroup(groupPos);
}else{
expandableList.expandGroup(groupPos);
}
}
Collapse all group except selected group in expandable listview android
Have the current expanded group position stored in a variable. In onGroupExpanded do the following.
private int lastExpandedPosition = -1;
private ExpandableListView lv; //your expandable listview
...
lv.setOnGroupExpandListener(new OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
if (lastExpandedPosition != -1
&& groupPosition != lastExpandedPosition) {
lv.collapseGroup(lastExpandedPosition);
}
lastExpandedPosition = groupPosition;
}
});
Programmatically Expand/Collapse Group
I just recently went through this myself and finally got it all working properly on my end. I posted my original question here along with my final solution to get everything working properly. Not sure if that will help you out, but it sounds REAL close to the issue that I was having and using my solution, I was able to get it working properly.
Toggle groups in expandable listview
There's no such thing out-of-box, but you can build it yourself pretty easily. You need to add the listener to collapse the previously openned group:
expListView.setOnGroupExpandListener( new OnGroupExpandListener() {
int previousGroup = -1;
@Override public void onGroupExpand( int groupPosition ) {
if( groupPosition != previousGroup ) expListView.collapseGroup( previousGroup );
previousGroup = groupPosition;
}
} );
Related Topics
Should I Use Android: Process =":Remote" in My Receiver
How to Create Converter for My Class in Android Retrofit Library
How to Notify Users About an Android App Update
Limit Height of Listview on Android
Displaying Yuv Image in Android
How to Create Expandable Listview in Flutter
How to Databind to Ontextchanged for an Edittext on Android
Add Google Maps API V2 in a Fragment
How to Display Menu Item with Icon and Text in Appcompatactivity
Position of Dialogfragment in Android
How to Remove Shared Preference While Application Uninstall in Android
Error:Unexpected Lock Protocol Found in Lock File. Expected 3, Found 0
How to Write Text Over a Picture in Android and Save It