ExpandableListView with non-expandable items

I am trying to create an ExpandableListView in my navbox that has other non-expandable options. I am trying to create something like below:


All cereals. ,,,,,, B


Archival crops. ,,,


Settings


Help & Feedback


About Us


All Cereals and Archived Cereals will have a dropdown indicator because they are groups with children, while Settings, About, etc. will not have an indicator. I went through everything and thought of two approaches: changing the adapter to inflate a different layout for empty groups and wrap the ExpandableListView in a regular ListView .

Changing the adapter turned out to be not as viable as I had hoped because the group indicator is automatically connected. I can't figure out how to turn off the auto-attach of the dropdown indicator. Help?

Stacking the ExpandableListView in a regular ListView was not as viable as I hoped because I couldn't get the XML to cooperate with two lists within the same DrawerLayout. Help?

+3


source to share


2 answers


If they are static items, then it is probably not worth the effort to implement something similar to ViewItemType. You might consider putting them in the footerView of the list.



Try listview.addFooterView (view);

+1


source


Your ExpandableListView will use an ExpandableListAdapter which will have some methods in it that will help you a lot. you can read more about them at this link http://developer.android.com/reference/android/widget/ExpandableListAdapter.html . Since some positions do not expand, what I have done in the past was overriding the getChildrenCount () method in ExpandableListAdaptor. Then, to specify the click behavior, it can be done by overriding getGroupViewMethod. Your adapter might 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 will do is that the items below the first 2 have no children. Since they have no children, they cannot expand. Click events will also only be processed on a conditional basis.

As for making the group indicator disappear, I would inflate the view from the xml in the getGroupView () method. This way you have full control over how it looks anyway. Hope this helps. Good luck.

0


source







All Articles