Use of ExpandableListView in android development to implement a list similar to QQ friends

Source: Internet
Author: User

Use of ExpandableListView in android development to implement a list similar to QQ friends

Due to work needs, I briefly studied ExpandableListView today and made a Demo similar to the QQ list to share with you.
As follows:

Let's take a look at the main layout file: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4NCjxwcmUgY2xhc3M9 "brush: java;">

Here we do not use the default separation line. The distance between the two groups is 8dp.

For ExpandableListView, the system provides a dedicated adapter BaseExpandableListAdapter. We can customize an adapter to inherit the BaseExpandableListAdapter and implement some methods in this class.
The Code is as follows:

Public class MyAdapter extends BaseExpandableListAdapter {private List
  
   
List; private Context context; public MyAdapter (List
   
    
List, Context context) {this. list = list; this. context = context;} public MyAdapter () {}@ Override public int getGroupCount () {return list. size () ;}@ Override public int getChildrenCount (int groupPosition) {return list. get (groupPosition ). getChildren (). size () ;}@ Override public Object getGroup (int groupPosition) {return list. get (groupPosition) ;}@ Override public Object getChild (int groupPosition, int childPosition) {return list. get (groupPosition ). getChildren (). 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 ;}@ Override public View getGroupView (int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {GroupHolder holder; if (convertView = null) {holder = new GroupHolder (); convertView = LayoutInflater. from (context ). inflate (R. layout. item_group, null); holder. title = (TextView) convertView. findViewById (R. id. group_title); holder. iv = (ImageView) convertView. findViewById (R. id. group_ico); convertView. setTag (holder);} else {holder = (GroupHolder) convertView. getTag ();} holder. title. setText (list. get (groupPosition ). getGroupName (); if (isExpanded) {holder. iv. setImageResource (R. drawable. rounds_open);} else {holder. iv. setImageResource (R. drawable. rounds_close);} return convertView;} @ Override public View getChildView (int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {ChildHolder holder; if (convertView = null) {holder = new ChildHolder (); convertView = LayoutInflater. from (context ). inflate (R. layout. item_child, null); holder. name = (TextView) convertView. findViewById (R. id. child_name); holder. sign = (TextView) convertView. findViewById (R. id. child_sign); convertView. setTag (holder);} else {holder = (ChildHolder) convertView. getTag ();} ChildBean cb = list. get (groupPosition ). getChildren (). get (childPosition); holder. name. setText (cb. getName (); holder. sign. setText ([Signature] + cb. getSign (); return convertView;} @ Override public boolean isChildSelectable (int groupPosition, int childPosition) {return false;} class GroupHolder {TextView title; ImageView iv ;} class ChildHolder {TextView name, sign ;}}
   
  

The code here is a little long. Let's explain it a little. First, two parameters are input in the constructor, one is the data source list, the other is the context, and the list is a GroupBean set. GroupBean is as follows:

public class GroupBean {    private String groupName;    private List
  
    children;    public String getGroupName() {        return groupName;    }    public void setGroupName(String groupName) {        this.groupName = groupName;    }    public List
   
     getChildren() {        return children;    }    public void setChildren(List
    
      children) {        this.children = children;    }    public GroupBean(String groupName, List
     
       children) {        this.groupName = groupName;        this.children = children;    }    public GroupBean() {    }}
     
    
   
  

Obviously, GroupBean has two attributes: group name and set of sub-items in the Group. ChildBean is the data of each object in each group. The ChildBean code is as follows:

public class ChildBean {    private String name;    private String sign;    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getSign() {        return sign;    }    public void setSign(String sign) {        this.sign = sign;    }    public ChildBean(String name, String sign) {        this.name = name;        this.sign = sign;    }    public ChildBean() {    }}

After listing, the following are getGroupCount () and getChildrenCount (), which are similar to the getCount () When BaseAdapter is used. The difference is that the number of each group is returned here, and the number of members in the group. size (), because the number of members in the group is different from each group, you must first obtain each group and then the number of members in the group. The code islist.get(groupPosition).getChildren().size();. The following two methods are:GetGroup () and getChild (), This is similarBaseAdapterIngetItemWhen we return the results, the group and the group subitem are returned separately. The code is very simple and I will not talk about it much. The longer method isGetGroupView and getChildViewThere are not many or no logic, and we use it in ListView.BaseAdapterIngetViewThe method is similar. The difference is that there is a difference when assigning values to Data.

I personally think thatExpandableListViewThe key is to clarify the data structure,Group and ChildThe rest is simple.

Here I will paste the Group layout and the Child layout for you to see:

Item_group.xml


  
      
       
        
     
    
   
  

Item_child.xml


  
      
       
        
     
    
   
  

A rounded corner box is also involved. The Code is as follows:
Item_background_select.xml


  
      
       
    
   
  

All the preparations have been completed. Let's take a look at how to use them.

Public class MainActivity extends Activity {private ExpandableListView mListView; private MyAdapter adapter; private List
  
   
List; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // initialize the data initData (); mListView = (ExpandableListView) this. findViewById (R. id. my_listview); adapter = new MyAdapter (list, this); mListView. setAdapter (adapter); mListView. setGroupIndicator (null); // mListView. expandGroup (0);} private void initData () {list = new ArrayList
   
    
(); {List
    
     
List1 = new ArrayList
     
      
(); ChildBean cb1 = new ChildBean (Mom, 123); ChildBean cb2 = new ChildBean (DAD, 456); ChildBean cb3 = new ChildBean (grandpa, 789 ); childBean cb4 = new ChildBean (Sister, 000); list1.add (cb1); list1.add (cb2); list1.add (cb3); list1.add (cb4); GroupBean gb1 = new GroupBean (home, list1); list. add (gb1) ;}{ List
      
        List1 = new ArrayList
       
         (); ChildBean cb1 = new ChildBean (Zhang San, 123); ChildBean cb2 = new ChildBean (Li Si, 456); ChildBean cb3 = new ChildBean (Wang Wu, 789 ); childBean cb4 = new ChildBean (Zhao liu, 000); ChildBean cb5 = new ChildBean (wind up, 1111); ChildBean cb6 = new ChildBean (maba, 222 ); childBean cb7 = new ChildBean (accommodation, 3333333); list1.add (cb1); list1.add (cb2); list1.add (cb3); list1.add (cb4); list1.add (cb5 ); list1.add (cb6); list1.add (cb7); GroupBean gb1 = new GroupBean (my friend, list1); list. add (gb1) ;}{ List
        
          List1 = new ArrayList
         
           (); ChildBean cb1 = new ChildBean (Tom, 123); ChildBean cb2 = new ChildBean (Jerry, 456); ChildBean cb4 = new ChildBean (Bush, 000 ); list1.add (cb1); list1.add (cb2); list1.add (cb4); GroupBean gb1 = new GroupBean (International friend, list1); list. add (gb1) ;}{ List
          
            List1 = new ArrayList
           
             (); ChildBean cb1 = new ChildBean (Zhao Gong, 123); ChildBean cb2 = new ChildBean (Machang, 456); ChildBean cb3 = new ChildBean (Wang Gong, 789 ); childBean cb4 = new ChildBean (, 000); ChildBean cb5 = new ChildBean (for example, 000); list1.add (cb1); list1.add (cb2); list1.add (cb3 ); list1.add (cb4); list1.add (cb5); GroupBean gb1 = new GroupBean (colleague, list1); list. add (gb1 );}}}
           
          
         
        
       
      
     
    
   
  

Here are two lines of code.mListView.setGroupIndicator(null);Do not use the expand and collapse icons provided by the system,mListView.expandGroup(0);Indicates that the first item is enabled by default.

Okay, let's talk about this. If you have any questions, please leave a message to discuss them.

 

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger. I am very grateful if you have any mistakes.

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.