ExpandableListView与Button结合使用

由于项目奇葩需求,ExpandableListView右端还要做一个button跳转到下一个ExpandableListView。本来ExpandableListView就可以显示两级了,但是还不符合要求。

首先Button如何添加进去的问题先,这个简单,在适配器group.xml布局里面添加就可以了

代码如下:


   
   

   
   
    
    
    
    
    
    
    
    
    


   
   

还有就是适配器的构造了

private void adapter(){
        List
   
   
    
     groupList = new ArrayList
    
    
     
     ();
		groupList.add(new String[]{"徐汇","123"});
		groupList.add(new String[]{"虹口","456"});
		// 创建一级条目
        List
     
     
      
      
       
       > groups = new ArrayList
       
        
        
          >(); //创建两个一级条目标题 Map 
         
           group1 = new HashMap 
          
            (); group1.put("group", groupList.get(0)[0]); Map 
           
             group2 = new HashMap 
            
              (); group2.put("group", "虹口"); groups.add(group1); groups.add(group2); // 创建一级条目下的的二级条目 List 
              
              
                > child1 = new ArrayList 
                
                
                  >(); //同样是在一级条目目录下创建两个对应的二级条目目录 Map 
                 
                   childdata1 = new HashMap 
                  
                    (); childdata1.put("child", "沪123"); Map 
                   
                     childdata2 = new HashMap 
                    
                      (); childdata2.put("child", "沪456"); child1.add(childdata1); child1.add(childdata2); //同上 List 
                      
                      
                        > child2 = new ArrayList 
                        
                        
                          >(); Map 
                         
                           childdata3 = new HashMap 
                          
                            (); childdata3.put("child", "沪789"); Map 
                           
                             childdata4 = new HashMap 
                            
                              (); childdata4.put("child", "沪741"); child2.add(childdata3); child2.add(childdata4); // 将二级条目放在一个集合里,供显示时使用 List 
                              
                               
                               
                                 >> childs = new ArrayList 
                                 
                                  
                                  
                                    >>(); childs.add(child1); childs.add(child2); /** * 使用SimpleExpandableListAdapter显示ExpandableListView * 参数1.上下文对象Context * 参数2.一级条目目录集合 * 参数3.一级条目对应的布局文件 * 参数4.fromto,就是map中的key,指定要显示的对象 * 参数5.与参数4对应,指定要显示在groups中的id * 参数6.二级条目目录集合 * 参数7.二级条目对应的布局文件 * 参数8.fromto,就是map中的key,指定要显示的对象 * 参数9.与参数8对应,指定要显示在childs中的id */ adapter = new LvButtonAdapter( this, groups, R.layout.group, new String[] { "group" }, new int[] { R.id.group }, childs, R.layout.child, new String[] { "child" }, new int[] { R.id.child }); } 
                                   
                                 
                                
                              
                             
                            
                           
                          
                         
                       
                     
                    
                   
                  
                 
               
             
            
           
          
        
      
      
    
    
   
   


接下来是最重要的,你点击button想处理那些数据,我处理的传groupid,我前面有给到它的。如groupList.add(new String[]{"徐汇","123"});。我要传的是123

这时候就要自己继承SimpleExpandableListAdapter。重写里面的getGroupView

public class LvButtonAdapter extends SimpleExpandableListAdapter { 
	    


		public LvButtonAdapter(Context context,
				List
   
   > groupData, int groupLayout,
				String[] groupFrom, int[] groupTo,
				List
   
   >> childData,
				int childLayout, String[] childFrom, int[] childTo) {
			super(context, groupData, groupLayout, groupFrom, groupTo, childData,
					childLayout, childFrom, childTo);
			// TODO Auto-generated constructor stub
		}

		private LayoutInflater mInflater; 
	    private Context mContext; 
	    
	    @Override
	    public View getGroupView(int groupPosition, boolean isExpanded,
	    		View convertView, ViewGroup parent) {
	    	mInflater=LayoutInflater.from(MainActivity.this);
	    	convertView = mInflater.inflate(R.layout.group, null) ; 
	    Button buttonClose = ( Button) convertView. findViewById(R.id.nextGroup) ; 
	        
	        buttonClose.setOnClickListener( new lvButtonListener( groupPosition ) ) ; 
	        return super.getGroupView(groupPosition, isExpanded, convertView, parent); 
	    } 

	    class lvButtonListener implements OnClickListener { 
	        private int position ; 

	        lvButtonListener( int pos) { 
	            position = pos; 
	        } 
	        
	        @Override 
	        public void onClick( View v) { 
	        	Toast.makeText(MainActivity.this, groupList.get(position)[1], Toast.LENGTH_LONG).show();
	        	startActivity(new Intent(MainActivity.this,NextActivity.class));
	        } 
	    } 
	}	


效果图如下:


有问题可以评论,有空我会上的。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值