How to perform ExpandableListView child click - android

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

Related

How to get key events on ExpandableListView

I am getting click event on ExpandableListView by overriting onChildClick. This is the code
public class SampleChooserActivity extends Activity
implements DownloadTracker.Listener, OnChildClickListener {
sampleListView.setOnChildClickListener(this);
#Override
public boolean onChildClick(
ExpandableListView parent, View view, int groupPosition, int childPosition, long id) {
// Child clicked
}
}
How can I get a similar event called another key is pressed on the child and not the click event?
This can be achieved by setting OnClickListener to the views in the child while inflating the child in getChildView() method of the adapter.

How to keep a child selection after collapse the expandable list view

How to keep a child selection after collapse the expandable list view. When the user selects one child in expandable list view after it collapse it will not shown ..
Implement onChild click listenr in your fragment/Activity
set the listener like below
expandableListView.setOnChildClickListener(this);
Code snippet:
#Override
public boolean onChildClick(ExpandableListView expandableListView, View view, int groupPosition, int childPosition, long id) {
YourView yourView = (YourView) view.findViewById(R.id.view_id);
YourDataItem item = (YourDataItem) yourView.getTag();
item.isSelected = !yourView.isChecked();
yourView.setChecked(item.isSelected);
}
Don't forget to setTag() in your adapter

how to get group having no child in ExpandableListView

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

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

How to get Group position from onChildClick()?

I have an ExpandableListView view and I've implemented OnChildClickListener, but I need to get the group of that child, more specifically - the text staying as a label of that group.
How can I do that?
Should I implement OnGroupClickListener?
Can somebody show me how to use getPackedPositionGroup() or any of the getPacked<somethingf>() methods to get to the text on the group element of expandable list view?
[Edit - addition]
the called is in Map class, which is something like this:
public class MapCommand extends Activity implements OnChildClickListener {
.....
#Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
firstStepSwitcher(v, childPosition);
///HERE IS WHERE I WANT TO FIND THE GROUP!!!
return true;
}
}
The view itself is in the layout XML. I do not manage its display manually.
And it has separate class for adapter.
What I did wrong was that I used to take the adapter for that ExpandableListView like this:
in (public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id))
parent.getAdapter();
But after looking into detail in the specification I found that I should take it like this:
ExpandableListAdapter mAdapter = parent.getExandableListAdapter();
from there on I do not have problem to get my group by calling:
String groupName = (String) mAdapter.getGroup(groupPosition);

Categories

Resources