ExpandableListView getGroupView friom child control - android

In an Expandablelistview, I want to update the one of the groupview image based on the selected control in the childview, how to accomplish this ??
basically
i want like this
public View getChildview(int groupPosition,final int childPosition, boolean isLastChild, View convertView,ViewGroup parent)
{
View Groupview=parent.getChildAt(groupPosition); ///but gives null when if scrolling the expandablelist
}
EDIT
Below Code works me
public View getGroupView(ExpandableListView listView, int groupPosition) {
long packedPosition = ExpandableListView.getPackedPositionForGroup(groupPosition);
int flatPosition = listView.getFlatListPosition(packedPosition);
int first = listView.getFirstVisiblePosition();
return listView.getChildAt(flatPosition - first);
}

I suggest using OnChildClickListener and override onChildClick() and use this code in getting the parent view.

getGroupView() – Returns view for the list group header
getChildView() – Returns view for list child item
So u must create/inflate view in getChildView method
For more details:
http://www.androidhive.info/2013/07/android-expandable-list-view-tutorial/

Related

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 check if group is expanded or collapsed in Android ExpandableListView?

I'm looking for an API like isExpanded() or isCollapsed() that tells me if a group is expanded or collapsed.
You can use isGroupExpanded .
expListViewObj.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
#Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
if(parent.isGroupExpanded(groupPosition))
{
// Do your Staff
}
else{
// Expanded ,Do your Staff
}
return false;
}
});
For more details you can visit Here
http://developer.android.com/reference/android/widget/ExpandableListView.html#setOnGroupClickListener(android.widget.ExpandableListView.OnGroupClickListener)
As #George D wrote there is ExpandableListView .isGroupExpanded(int groupPosition) method. Well you could add the code to get expanded group position or -1
public int getExpandedGroupPosition() {
for (int i = 0; i < listView.getExpandableListAdapter().getGroupCount(); i++) {
if ( listView.isGroupExpanded(i) ) {
return i;
}
}
return -1;
}
If u use BaseExpandableListAdapter class you have getGroupView() override method.
public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
// Here is your expand list view parent id
// b specify that parent expand or not.
.............
if(b){
imdDownArrow.setVisibility(View.GONE);
imdUpArrow.setVisibility(View.VISIBLE);
}else{
imdDownArrow.setVisibility(View.VISIBLE);
imdUpArrow.setVisibility(View.GONE);
}
...............
return view;
}
so using this simple code you can show arrow up or down
There is a parameter in getGroupView() on your ExpandableListAdapter, a boolean that represents wheter the group is expanded or not.
From (http://developer.android.com/reference/android/widget/ExpandableListAdapter.html#getGroupView(int, boolean, android.view.View, android.view.ViewGroup)
Gets a View that displays the given group. This View is only for the group--the Views for the group's children will be fetched using getChildView(int, int, boolean, View, ViewGroup)).
Parameters
groupPosition the position of the group for which the View is returned
isExpanded whether the group is expanded or collapsed
convertView the old view to reuse, if possible. You should check that this view is non-null and of an appropriate type before using. If it is not possible to convert this view to display the correct data, this method can create a new view. It is not guaranteed that the convertView will have been previously created by getGroupView(int, boolean, View, ViewGroup).
parent the parent that this view will eventually be attached to

how to get view of group in expandablelistview in android?

im blazilian so, my english is not good.
So.. i need get view of group in expandablelistview for get your object tag throught view.getTag() method.
follow me in this example:
ExpandableListView
--> group (i need this view)
----> child
----> child
----> child
--> group (i need this view)
----> child
----> child
My code:
#Override
public boolean onChildClick(final ExpandableListView parent, final View v,
final int groupPosition, final int childPosition, final long id) {
/* I NEED GET VIEW OF GROUP FOR GET YOUR TAG*/
View vParent = parent.getChildAt(groupPosition); // dont work after first group
Programa v2 = (Programa) parent.getTag(); // return null
// v parameter is a child of group
return true;
}
in my adapter:
#Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
TwoLineListItem view = (TwoLineListItem) LayoutInflater.from(contexto)
.inflate(android.R.layout.simple_expandable_list_item_2,
parent, false);
String programa = map.keySet().toArray(new String[map.keySet().size()])[groupPosition];
view.getText1().setText(programa);
view.getText2().setText("PROGRAMA LOCAL");
view.setTag(programas.get(groupPosition)); // i need get this in child click listener
return convertView = view;
}
any idea? thanks
To get the group view from an ExpandableListView, you do something as follows:
public View getGroupView(ExpandableListView listView, int groupPosition) {
long packedPosition = ExpandableListView.getPackedPositionForGroup(groupPosition);
int flatPosition = listView.getFlatListPosition(packedPosition);
int first = listView.getFirstVisiblePosition();
return listView.getChildAt(flatPosition - first);
}
If your code is correct, you want to get the group view for the child so that you can call getTag() on it, correct?
If so, why not just skip that step and access the value of the tag, set by programas.get(groupPosition) manually?
You can do this by calling:
programas.get(groupPosition) right on the top of your onChildClick method since you get the group position value there too.
Edit in response to your comment:
The issue here is that you're not going to be able to get the view of the group through the adapter since it might involve recreating the view due to recycling of views in lists. If this method doesn't work, I strongly suggest modifying your code to make it work.
If programas is one of the inputs to your adapter, then call getGroup(groupPosition) on your adapter to access it. Else, make a public method in your adapter class to allow retrieval of that value.
I tried AedonEtLIRA answer but it only obtained the first item in the list.
My solution was to set a tag in my adapter's getGroupView method.
#Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.listitem_group, null);
}
//This is your data object that you might be using for storage that can be returned by your getGroup(groupPosition) function
GroupDataDto groupDataDto = (GroupDataDto) getGroup(groupPosition);
String name = groupDataDto.getName();
convertView.setTag(name);
...
Now in your Fragment / Activity, in your onGroupExpand and onGroupCollapse:
GroupDataDto groupDataDto = (GroupDataDto) listAdapter.getGroup(groupPosition);
ImageView arrow = (ImageView) getView().findViewWithTag(groupDataDto.getName()).findViewById(R.id.expandable_arrow_down);
Then I can do my animation on the arrow, which is why I wanted to get that group view.
Well I used the following code to access a TextView in a group:
#Override
public boolean onChildClick(final ExpandableListView parent, final View v,
final int groupPosition, final int childPosition, final long id) {
View vParent = mAdapter.getGroupView(groupPosition, true, null, null);
Programa v2 = (Programa) vParent.getTag();
return true;
}
I hope it helps

Android BaseExpandableListAdapter - How does the getView method work

I am having a ExpandableList that extands a adapter BaseExpandableListAdapter .
I want to change the child view when child view gets clicked
the adding of a children in ExpandableListView gets done by :
#Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, final View convertView, ViewGroup parent) {}
How do I change childs when one of them gets clicked?
use OnChildClickListener.
don't change childview directly, just modify the data associated with the child and, refresh the listview by something like notifyDatasetChanged()

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