Reputation: 1586
I'm trying to create an ExpandableListView in my navigation drawer that has other, non-expandable options. I'm trying to create something like below:
All cereals. . . . . . . . . . . . V
Archived Cereals . . . . . . V
Settings
Help and Feedback
About Us
"All cereals" and "Archived cereals" would have the dropdown indicator because they are groups with children, while "Settings," "About Us," etc. wouldn't have the indicator. I've looked all over and thought of two approaches: modifying the adapter to inflate a different layout for empty groups, and stacking an ExpandableListView on a normal ListView.
Modifying the adapter was not as viable as I'd hoped because the group indicator is attached automatically. I can't figure out how to make the automatic attachment of the dropdown indicator go away. Help?
Stacking the ExpandableListView on a normal ListView was not as viable as I'd hoped, either, because I couldn't get the XML to cooperate with two lists within one DrawerLayout. Help?
Upvotes: 4
Views: 1890
Reputation: 503
Your ExpandableListView will use an ExpandableListAdapter, which will have some methods in it that will help you out a lot. you can read more about them at this link http://developer.android.com/reference/android/widget/ExpandableListAdapter.html. Starting with having some of the positions not expand, what I have done in the past was override the getChildrenCount() method in the ExpandableListAdaptor. Then to specify click behavior, that can be done by overriding the getGroupViewMethod. Your adapter could look something like this:
public class MyAdapter extends BaseExpandableListAdapter {
...
@Override
//This is where we tell the list not to expand on any position beyond the first 2
public int getChildrenCount(int position) {
switch(position)
{
case 0:
//Expands with the size of the cereal array
return cerealArray.size();
case 1:
//Expands with the size of the Archived Cereal Array
return archivedCerealArray.size();
default:
//List does not expand
return 0;
}
...
@Override
public View getGroupView(int groupPosition,boolean isExpanded,View convertView,ViewGroup parent)
{
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.kat_layout, null);
}
ImageView arrow = (ImageView)convertView.findViewById(R.id.arrowImage);
//Test for position
switch (groupPosition)
{
case 0:
//set arrow visibility to visible
arrow.setVisibility(View.VISIBLE);
break;
case 1:
//set arrow visibility to visible
arrow.setVisibility(View.VISIBLE);
break;
case 2:
//hide arrow
arrow.setVisibility(View.INVISIBLE);
convertView.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v){
//Navigate to settings
}
}
break;
case 3:
//hide arrow
arrow.setVisibility(View.INVISIBLE);
convertView.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v){
//Help and feedback
}
}
break;
case 4:
//hide arrow
arrow.setVisibility(View.INVISIBLE);
convertView.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v){
//About us
}
}
break;
}
return convertView;
}
what that will do is have the elements below the first 2 have no children. Since they have no children, they cannot expand. The click events will also be handled only on condition of position.
As far as getting the group indicator to go away, I would inflate a view from xml in the getGroupView() method. That way, you have complete control over how it looks in any case. I hope this helps. Good Luck.
Upvotes: 0
Reputation: 352
If those are static elements, then its probably not worth the effort implementing something similar to ViewItemType. You can consider putting them in the footerView of the listview.
Try listview.addFooterView(view);
Upvotes: 2