how to get group having no child in ExpandableListView - android

I am using Custom ExpandableListview for first time. I have custom expandable listview which contains 10 groups in which some groups have child and some groups don't have child.
I want child when the child item is clicked. (in case of groups having child)
I want group when the group item is clicked. (in case of groups don't have child)
I am getting child by using OnChildClickListener(first case) , but I didn't getting group. (second case)
Is there any way to get both at a time. If group have child then return clicked child otherwise return clicked group.
Please help me

First set OnChildClickListener to list
Next set OnGroupExpandListener to list
exList.setOnChildClickListener(this);
exList.setOnGroupExpandListener(this);
#Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
Group gr = ds_list.get(groupPosition);
//here you can get child from group
return true;
}
#Override
public void onGroupExpand(int i) {
if(group don't have child) {
//get group by using group position(i)
}
}
You can add OnGroupCollapseListener also
Don't use OnChildClickListener and OnGroupExpandListener together because group items are not expandable behaves like a normal ListView

You can use this method to get the index of parent view, and check whether the parent in that position contains a child or not.
expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
#Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
//here you can check whether your parent contains a child or not by getting the parent position.
return false;
}
});

Related

expand all view when activity is load in ExpandableListView

My problem is...
I use Expandable list view in my activity.
When the activity created it shows all the child group is collapsed.
I would like to expand all children while the expandable list view is populated.
like
for that, I used below code in getGroupView() method adapter class
ExpandableListView eLV = (ExpandableListView) parent
eLV.expandGroup(groupPosition,true);
now the groups are expanded but it can not allow me to collapse by clicking on the group header.
Simply add groupClickListener and return true for disabling collapse the child list. Add this code to your activity.
yourExpandableList.setOnGroupClickListener(new OnGroupClickListener() {
#Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
return true; // This way the expander cannot be collapsed
}
});

How to perform ExpandableListView child click

I create an Expandable ListView which contains 3 group and each group have several child.
In OnCreate i expand group one with exLv.expandGroup(0); then i want to click child of this group without user touch it (dynamically).
I tried exLv.setSelectedChild and it didn't work.
I just want to do this one time at the onCreate. How to do this?
In your activity class override the OnChildClickListener() method from list view.
expandbleList.setOnChildClickListener(new OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
//do something here.
}
});

How to make an ExpandableListView in Android always show child items

I want to make an ExpandableListView which should always be showing its children elements
even after clicking the header, and it should not collapse. How could I implement this concept?
you can expand all groups one by one as below as mentioned in this post
ExpandableListView explst;
explst.expandGroup(0);
explst.expandGroup(1);
for disabling collapse you can define a OnGroupClickListener which returns true, like so: (as mentioned in this post)
explst.setOnGroupClickListener(new OnGroupClickListener() {
#Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
return true; // This way the expander cannot be collapsed
}
});

In Android Expanded List, how do I get the current view when group is expanded?

So I have an Expanded List:
ExpandableListView expListView;
I setup a group expand listner, which I plan to change some image inside the group view based on group expanded status.
// Listview Group expanded listener
expListView.setOnGroupExpandListener(new OnGroupExpandListener() {
#Override
public void onGroupExpand(int groupPosition) {
if (curExpandedGeoArea >= 0 && groupPosition != curExpandedGeoArea) {
expListView.collapseGroup(curExpandedGeoArea);
// How do I get the view of the current expanded view?
}
curExpandedGeoArea = groupPosition;
}
});
My question is, in the onGroupExpand(int groupPosition) function, how do I get the View of the expanded group?
I read other questions, and it is suggested I use the
onGroupClick(ExpandableListView parent, View v, int groupPosition, long id)
function, which contain View v, the problem with that approach is that I might expand my group programmatically, so that function is not called, what should I do?
in getGroupView() method of ExpandableListAdapter. You can check whether current group is expanding or not using isExpanded parameter of getGropuView and change whatever icon you want to change.

need help to custom expandableListView

I am new to android, and I want to implement a ListView like the image below. It seems expandableListView may meet my request, but what I want is only Item 4 has children and can be expanded, 'Item 1' is just an option user can click, and can't be expanded. I read the document of Expandable List View, and it's not clear enough how to implement this. Can anyone gave some suggestion on implementing this? Thank you so much
but what I want is only Item 4 has children and can be expanded
If Item doesn't have children, it won't be exanded. You can image that like:
public class Item {
private String title;
private List<Option> children; // if size = 0 won't be expanded
}
It's native behaviour of ExpandableListView so if any Item doesn't have children, implicitly won't be expanded but you need to set OnClickListener for both groups (Items) and children (Options):
public boolean onChildClick(ExpandableListView parent, View row,
int groupPosition, int childPosition, long id) {
// callback method after click for Options
return false;
}
public boolean onGroupClick(ExpandableListView parent, View row,
int groupPosition, long id) {
/** if you are using BaseAdapter subclass implementation **/
adapter.getGroup(groupPosition)).getChildren() == null) {
// item doesn't have children
}
else {
// has children
}
Generally if you want to get more control over your ListView i pretty recommend you to implement own subclass of ListAdapter in your case you can use BaseExpandableListAdapter.
And an appearance of List is only issue about proper styled layouts for example removing group indicator with
android:groupIndicator="#null"
Update:
Issue mentioned by #Roberto Lombardini:
A little problem with this solution is that the little grey triangle
indicating the state of the groupview (expanded or not) will appear
even if group has no children. Am i wrong?
If you want to have gray triangle only for Items with children, you need to fix it in your custom adapter in getGroupView() method:
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
// inflating View etc.
Item item = items.get(groupPosition);
if (item != null) {
// if Item has children
if (item.getChildren() != null && !item.getChildren().isEmpty()) {
// set children indicator to VISIBLE
imageView.setVisibility(ImageView.VISIBLE);
}
// if Item doesn't have children
else {
// set children indicator to GONE
imageView.setVisibility(ImageView.GONE);
}
}
}
Try to implement some custom Adapter, I think could be the better way to do what you need
lv.setOnGroupClickListener(new OnGroupClickListener() {
public boolean onGroupClick(ExpandableListView parent, View v, int position, long id) {
switch (position) {
case 0: return true;
case 2: return true; // returning true wont expand
default:
return false;// if you return false then only that position will be expanded
}
}
}
);
just return false on the which parent position you want to expand and in which you dont want to expand you can return true.

Categories

Resources