ExpandableListView可折叠的列表

本文介绍了一种使用Java实现的适配器模式方法,通过创建一个适配器类来处理ExpandableListView的数据绑定,该适配器实现了BaseExpandableListAdapter接口,并详细说明了如何为ExpandableListView设置监听器。

创建适配器 实现4个方法:getGroupCount()/ getGroupView() getChildCount()/ getChildView()

public class MyAdapter extends BaseExpandableListAdapter {

         private ArrayList<String> mGroupNames;   
         private HashMap<String, ArrayList<String>> mChildNames;

         public MyAdapter(ArrayList<String> groupNames,    HashMap<String, ArrayList<String>> childNames){          
               mGroupNames=groupNames;  
               mChildNames=childNames;  
          }

          @Override    
          public int getGroupCount() { 
              return mGroupNames.size();  
          }

@Override 
        public View getGroupView(int groupPosition, boolean isExpanded,    View convertView, ViewGroup parent) {   
              LayoutInflater inflater = LayoutInflater.from(parent.getContext());    
              View itemView = inflater.inflate(android.R.layout.simple_list_item_1, null);    
              TextView tv = (TextView) itemView.findViewById(android.R.id.text1);                     tv.setText(mGroupNames.get(groupPosition));      
              return itemView;   
         }

        @Override  
        public int getChildrenCount(int groupPosition) {        //1.获取组        
            String groupName = mGroupNames.get(groupPosition); //2.根据组名获取⼦子列表  
            ArrayList<String> childNames = mChildNames.get(groupName); 
            return childNames.size();  
        }

        @Override    
        public View getChildView(int groupPosition, int childPosition,    boolean isLastChild, View convertView, ViewGroup parent) {   

             LayoutInflater inflater = LayoutInflater.from(parent.getContext()); 
             View itemView = inflater.inflate(android.R.layout.simple_list_item_1, null);       

             //1.控件已经被获取了  
             TextView tv = (TextView) itemView.findViewById(android.R.id.text1);        

             //2.获取⼦子视图的数据     

             //3.获取组     
             String groupName = mGroupNames.get(groupPosition);       

             //4.根据组名获取⼦子列表       
             ArrayList<String> childNames = mChildNames.get(groupName);  

             //5.从⼦子列表⾥里⾯面获取某个Item       
             String text = childNames.get(childPosition);      

             tv.setText(text);        
             return itemView;  

        }

}

 

1.在Activity的onCreate中创建数据 并将数据绑定到列表中

2.设置列表监听器 (组折叠/展开/点击监听器 子视图点击的监听器)

public class MainActivity extends Activity {
   private ExpandableListView mExpLv;

        @Override  
   protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);        
        setContentView(R.layout.activity_main);

        mExpLv=(ExpandableListView) findViewById(R.id.explv);
        //1.组的数据  
        ArrayList<String> groupNames=new ArrayList<String>();
        for (int i = 0; i < 3; i++) {          
             groupNames.add("组 "+i);    
        }        
        //2.成员的数据  
         HashMap<String, ArrayList<String>> childNames=new HashMap<String, ArrayList<String>>();  
         for (int i = 0; i < groupNames.size(); i++) {        
             ArrayList<String> children=new ArrayList<String>();    
             for (int j = 0; j < 5; j++) {        
                 children.add("成员"+j);    
             }        
             //遍历下组名 key就是组名 值就时⼀一个成员队列          
             childNames.put(groupNames.get(i), children);    
          }

          MyAdapter adapter=new MyAdapter(groupNames,childNames);  
          mExpLv.setAdapter(adapter);
          //为ExpandableListView的组设置折叠监听器      
          //  mExpLv.setOnGroupCollapseListener(onGroupCollapseListener)      
          //  mExpLv.setOnGroupExpandListener(onGroupExpandListener)      
          //  mExpLv.setOnGroupClickListener(onGroupClickListener)      
          //  mExpLv.setOnChildClickListener(onChildClickListener)
          mExpLv.setOnGroupExpandListener(new OnGroupExpandListener() {
              @Override  
              public void onGroupExpand(int groupPosition) {          
                  Toast.makeText(MainActivity.this, groupPosition+" 被展开了", 0).show();   
              }  
         });
    } 
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

达帮主

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值