I am beginner to android I need some Help about List and Extended List in one Drawer
I know that this question is posted here several Time but I am unable to understand that Actually I want to make a list like:
Please Help me Step by Step
thak you!...
It's native behavior 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 **/
if(adapter.getGroup(groupPosition).getChildren() == null) {
// item doesn't have children
return true;
}
else {
// has children
return false;
}
}
Related
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;
}
});
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.
}
});
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.
Hi guys I am trying to change the value of the parent by clicking a child in a expandable list. I have looked for a solution but cant find anything.
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id){
ExpandableListAdapter itemAdapter = parent.getExpandableListAdapter();
String selectedItem = (String)itemAdapter.getChild(groupPosition, childPosition);
groupHeader(selectedItem);
if(parent.isGroupExpanded(groupPosition)){
parent.collapseGroup(groupPosition);
}
return true;
}
});
An ExpandableListView depends on an adapter to obtain its values. Those values come from some sort of data structure, often an ArrayList or even a simple array. In your onChildClick(), pass a reference to the data structure used to build your ExpandableListAdapter and modify the data structure directly, followed by a call to notifyDataSetChanged().
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id)
{
ExpandableListAdapter itemAdapter = parent.getExpandableListAdapter();
String selectedItem = (String)itemAdapter.getChild(groupPosition, childPosition);
groupHeader(selectedItem);
if(parent.isGroupExpanded(groupPosition))
{
parent.collapseGroup(groupPosition);
}
// your logic here
// expandableListArrayList.add() or expandableListArrayList.remove()
// or whatever
itemAdapter.notifyDataSetChanged();
return true;
}
});
You probably have to extend the adapter to create the functionality you need. Your question is a bit nebulous because I can't see the overall structure of your code, but this should give you a running start.
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);