I have ExpandableListview inside NavigationDrawer. When group is expanded I collapse the previous expanded group, but when group is collapsed expanded group loses the focus (position of ExpandableListview goes down). Here's the code:
#Override
public boolean onGroupClick(ExpandableListView expandableListView, View view, int i, long l) {
if (mListView.isGroupExpanded(i)) {
mListView.collapseGroupWithAnimation(i);
} else {
mListView.expandGroupWithAnimation(i);
if(i!=lastExpandedGroupPosition )
mListView.collapseGroupWithAnimation(lastExpandedGroupPosition);
lastExpandedGroupPosition = i;
}
return true;
}
Use the function setSelectedGroup(groupPosition) with the expandablelistview object while expanding the list. This would set the focus for the group , who's view you had clicked!
Happy coding :)
Related
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
}
});
I have an expandable list view comprising of textview and imageview as parent and textview as child. Is there a way to set typeface of textview(either in parent or child layout) based upon the selection made?
I want to change typeface to bold when a user selects an item from the list. I searched a lot but we have only group click or child click listeners. getSelectedItem() or getSelectedItemPosition() doesn't give appropriate result. Please suggest
1) you can check expListView.isGroupExpanded(groupPosition) and provide conditions for changing typeface accordingly.
2) you can also do it like this :
expListView.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;
}
});
3) you can do it in getGroupView just check the boolean isGroupExpanded and do your stuff.
I want to create a ListView with a header view that can be expanded and retractet just as in a ExpandableListView (but just the header view, all other views just be normal list items).
I'm using an ExpandableListView with a BaseExpandableListAdapter and treat all the other items in the list as groups that have no children.
This is cumbersome and error-prone and has unwanted sideeffects.
I also tried using a normal ListView and a view for the header where I setVisivility(View.VISIBLE) to the expanded part of the view when the header view is clicked, but this doesn't seem to have any effect:
private View getHeaderView() {
final View v = layoutInflater.inflate(R.layout.headertest, null);
View v1 = v.findViewById(R.id.view1);
final View v2 = v.findViewById(R.id.view2);
v1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.d("head", "click");
v2.setVisibility(View.VISIBLE);
notifyDataSetChanged();
}
});
return v;
}
When v1 is clicked, the visibility of v2 is not changed, although the clickListener is called. I also tried .invalidate() on all kinds of views, to no avail.
Any ideas on how to solve this one or the other way?
You can do it with the expandable list view. First disable the indicators (open/close icons) on your ListView:
mExpList.setIndicator(null);
Next, your header (first group) will have to be a special type of group which has an image (an indicator) in its layout. Basically you want to have 2 types of group views:
#Override
public int getGroupTypeCount() {
return 2;
}
#Override
public int getGroupType(int groupPosition) {
if (groupPosition == headerPosition) {
return 1;
} else {
return 0;
}
}
You will also need to Override onGroupExpanded and onGroupCollapsed to change the image of your header (if a header was clicked of course).
Lastly you'll need to override the ListView's OnGroupClickListener:
mExpList.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
#Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
if (groupPosition == header) {
// Click was not handled
return false;
} else {
// Do some normal stuff with items
// Click was handled group won't be (or tried to be) expanded
return true;
}
}
});
Also a good idea would be to override adapters getChildCount to return 0 when it's not a header.
Hope this helps. Good luck!
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.
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.