Sunday, February 14, 2016

ExpandableListView

I'm building an ExpandableListView in Android.  It's like making an outline that shows/hides subcategories of main categories.  Here's the expandable list view when it's unexpanded:
  • Coffee
  • Tea
  • Juice
Here's expandable list view when it's expanded:
  • Coffee
    • light roast
    • medium roast
    • dark roast
  • Tea
    • Chamomile
    • Spearmint
    • Darjeeling
  • Juice
    • Pineapple
    • Grapefruit
    • Guava
A click on a category expands the list beneath the heading.

It's simple, right?  Except in Java and Android there's a lot of parts.

Here are my notes:

An ExpandableListView is a list that holds lists.  The main lists are the headers.  They're displayed with some kind of indicator next to them to show that they contain more detail inside.

Parts needed:

  1. XML
    1. Layouts
      1. Main layout - define the main container.
        1. LinearLayout
          1. ExpandableListView
      2. Main header layout - format text/background for categories.
        1. LinearyLayout
          1. TextView
      3. Item header layout - format text/background for items.
        1. LinearLayout
          1. TextView
  2. Java
    1. Class that extends BaseExpandableListAdapter:
      1. class DrinkMenuExpandableListAdapter extends BaseExpandableListAdapter
    2. An Activity or Fragment 
      1. To prepare the lists data.
      2. To instantiate the class that extends BaseExpandableListAdapter, passing it the data.
      3. To connect listeners:
        1. setOnChildClickListener
        2. setOnGroupClickListener
        3. setOnGroupExpandedListener
        4. setOnGroupCollapseListener
        5. setOnItemClickListener
I'll add the XML code here along with the Java.  Will also look at putting it on Github.

No comments:

Post a Comment