Android basic getting started -- 2.4.12 basic use of ExpandableListView (foldable List)

Source: Internet
Author: User

Android basic getting started -- 2.4.12 basic use of ExpandableListView (foldable List)
This section introduces:

The Adapter controls described in this section are ExpandableListView, which is a foldable list and a subclass of ListView,
Based on ListView, it divides the list items in the application into several groups. Each group can contain multiple list items. As you can see,
Similar to the QQ contact list, its usage is very similar to that of ListView, but the list items displayed by ExpandableListVivew
It must be provided by ExpandableAdapter. Next we will learn the basic usage of this control!
Official API: ExpandableListView

1. related attributes
Android: childDivider: Specify the separator between sub-table items in each group. The picture is not completely displayed,
The list of ions is a straight line. Android: childIndicator: Drawable object displayed next to the sublist. It can be an image. Android: childIndicatorEnd: End constraint position of the sub-list item indicator Android: childIndicatorLeft: Left constraint position of the sub-list item indicator Android: childIndicatorRight: Constraint position on the right of the sub-list item indicator Android: childIndicatorStart: The starting constraint position of the sub-list item indicator Android: groupIndicator: Drawable object displayed next to the group list. It can be an image. Android: indicatorEnd: End constraint position of the group list item indicator Android: indicatorLeft: Left constraint position of the group list item indicator Android: indicatorRight: Constraint position on the right of the group list item indicator Android: indicatorStart: Start constraint position of the group list item indicator
2. Three methods to implement ExpandableAdapter

1.ExtensionBaseExpandableListAdpterImplement ExpandableAdapter.
2.UseSimpleExpandableListAdpaterWrap two List sets into ExpandableAdapter
3.UseSimpleCursorTreeAdapterWrap data in Cursor into SimpleCuroTreeAdapter
In this example, we use the first extension, which is BaseExpandableListAdpter. We need to override the related methods in this class,
The following is a sample code!

3. Sample Code

Let's take a look at the implementation:

The following figure shows the effect:

The core is rewriting.BaseExpandableListAdpterIn fact, it is similar to the common BaseAdapter previously written,
However, BaseExpandableListAdpter is divided into two parts: group and sub-list. You will know the specific code!
In addition, rewriteIsChildSelectable ()The method must return true, otherwise it will not be triggered.
Subitem click event! Let's write:

First, the layout of the group and sub-list:

Item_exlist_group.xml:


  
      
   
  

Item_exlist_item.xml:


  
      
       
    
   
  

Then there is the custom Adapter class:

MyBaseExpandableListAdapter. java:

/*** Created by Jay on 0025. */public class MyBaseExpandableListAdapter extends BaseExpandableListAdapter {private ArrayList
  
   
GData; private ArrayList> iData; private Context mContext; public MyBaseExpandableListAdapter (ArrayList
   
    
GData, ArrayList> iData, Context mContext) {this. gData = gData; this. iData = iData; this. mContext = mContext;} @ Override public int getGroupCount () {return gData. size () ;}@ Override public int getChildrenCount (int groupPosition) {return iData. get (groupPosition ). size () ;}@ Override public Group getGroup (int groupPosition) {return gData. get (groupPosition) ;}@ Override public Item getChild (int groupPosition, int childPosition) {return iData. get (groupPosition ). get (childPosition) ;}@ Override public long getGroupId (int groupPosition) {return groupPosition ;}@ Override public long getChildId (int groupPosition, int childPosition) {return childPosition ;} @ Override public boolean hasStableIds () {return false;} // retrieves a view for displaying a given group. this method returns only the View object @ Override public View getGroupView (int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {ViewHolderGroup groupHolder; if (convertView = null) of the group) {convertView = LayoutInflater. from (mContext ). inflate (R. layout. item_exlist_group, parent, false); groupHolder = new ViewHolderGroup (); groupHolder. TV _group_name = (TextView) convertView. findViewById (R. id. TV _group_name); convertView. setTag (groupHolder);} else {groupHolder = (ViewHolderGroup) convertView. getTag ();} groupHolder. TV _group_name.setText (gData. get (groupPosition ). getgName (); return convertView;} // obtain the View for displaying the data of the given group to the sub-position @ Override public View getChildView (int groupPosition, int childPosition, boolean isLastChild, view convertView, ViewGroup parent) {ViewHolderItem itemHolder; if (convertView = null) {convertView = LayoutInflater. from (mContext ). inflate (R. layout. item_exlist_item, parent, false); itemHolder = new ViewHolderItem (); itemHolder. img_icon = (ImageView) convertView. findViewById (R. id. img_icon); itemHolder. TV _name = (TextView) convertView. findViewById (R. id. TV _name); convertView. setTag (itemHolder);} else {itemHolder = (ViewHolderItem) convertView. getTag ();} itemHolder. img_icon.setImageResource (iData. get (groupPosition ). get (childPosition ). getiId (); itemHolder. TV _name.setText (iData. get (groupPosition ). get (childPosition ). getiName (); return convertView;} // sets whether the sublist is optional @ Override public boolean isChildSelectable (int groupPosition, int childPosition) {return true ;} private static class ViewHolderGroup {private TextView TV _group_name;} private static class ViewHolderItem {private ImageView img_icon; private TextView TV _name ;}}
   
  

PS: ArrayList is not required to store sub-List data.


      
   
  

MainActivity. java:

Public class MainActivity extends AppCompatActivity {private ArrayList
  
   
GData = null; private ArrayList> iData = null; private ArrayList
   
    
LData = null; private Context mContext; private ExpandableListView exlist_lol; private MyBaseExpandableListAdapter myAdapter = null; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); mContext = MainActivity. this; exlist_lol = (ExpandableListView) findViewById (R. id. exlist_lol); // data preparation gData = new ArrayList
    
     
(); IData = new ArrayList> (); gData. add (new Group (AD); gData. add (new Group (AP); gData. add (new Group (TANK); lData = new ArrayList
     
      
(); // AD group lData. add (new Item (R. mipmap. iv_lol_icon3, jiansheng); lData. add (new Item (R. mipmap. iv_lol_icon4, delaven); lData. add (new Item (R. mipmap. iv_lol_icon13, gun); lData. add (new Item (R. mipmap. iv_lol_icon14, verus); iData. add (lData); // AP group lData = new ArrayList
      
        (); LData. add (new Item (R. mipmap. iv_lol_icon1, Timo); lData. add (new Item (R. mipmap. iv_lol_icon7, Anne); lData. add (new Item (R. mipmap. iv_lol_icon8, Angel); lData. add (new Item (R. mipmap. iv_lol_icon9, zelas); lData. add (new Item (R. mipmap. iv_lol_icon11, Fox); iData. add (lData); // TANK group lData = new ArrayList
       
         (); LData. add (new Item (R. mipmap. iv_lol_icon2, novices); lData. add (new Item (R. mipmap. iv_lol_icon5, Debon); lData. add (new Item (R. mipmap. iv_lol_icon6, OLAF); lData. add (new Item (R. mipmap. iv_lol_icon10, Dragon girl); lData. add (new Item (R. mipmap. iv_lol_icon12, bear); iData. add (lData); myAdapter = new MyBaseExpandableListAdapter (gData, iData, mContext); exlist_lol.setAdapter (myAdapter); // set the Click Event (new ExpandableListView) for the list. onChildClickListener () {@ Override public boolean onChildClick (ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {Toast. makeText (mContext, you clicked: + iData. get (groupPosition ). get (childPosition ). getiName (), Toast. LENGTH_SHORT ). show (); return true ;}});}}
       
      
     
    
   
  
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.