Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android ExpandableListView詳解

android ExpandableListView詳解

編輯:關於Android編程

點擊顯示展開項,先看效果:

\\

開始,1.先搞個XML顯示主界面:

main_activity.xml:

 

2.進入main_activity開始搞顯示工作:

 

//定義兩個List用來控制Group和Child中的String;
	
	private  List  groupArray;//組列表
    private  List> childArray;//子列表
    private  ExpandableListView  expandableListView_one;

初始化數據:

 

 

	//初始化數據
	private void initdate() 
    {
        addInfo("語言", new String[]{"Oracle","Java","Linux","Jquery"});
        addInfo("男人的需求", new String[]{"金錢","事業","權力","女人","房子","車","球"});
    }
	
	//增加數據到列表中去
	private void addInfo(String group,String []child) {
        groupArray.add(group);//增加到外租列表中
        List  childItem =new ArrayList();
        for(int index=0;index

 

初始化之後,可進行適配器適配:

 

 expandableListView_one.setAdapter(new ExpandableListViewaAdapter(MainActivity.this));
 class ExpandableListViewaAdapter extends BaseExpandableListAdapter {
	        Activity activity;
	         public  ExpandableListViewaAdapter(Activity a)  
	            {  
	                activity = a;  
	            }  
	       /*-----------------Child */
	        @Override
	        public Object getChild(int groupPosition, int childPosition) {
	            // TODO Auto-generated method stub
	            return childArray.get(groupPosition).get(childPosition);
	        }
	 
	        @Override
	        public long getChildId(int groupPosition, int childPosition) {
	            // TODO Auto-generated method stub
	            return childPosition;
	        }
	 
	        @Override
	        public View getChildView(int groupPosition, int childPosition,
	                boolean isLastChild, View convertView, ViewGroup parent) {
	             
	            String string =childArray.get(groupPosition).get(childPosition);
	             
	            return getGenericView(string);
	        }
	 
	        @Override
	        public int getChildrenCount(int groupPosition) {
	            // TODO Auto-generated method stub
	            return childArray.get(groupPosition).size();
	        }
	       /* ----------------------------Group */
	        @Override
	        public Object getGroup(int groupPosition) {
	            // TODO Auto-generated method stub
	            return getGroup(groupPosition);
	        }
	 
	        @Override
	        public int getGroupCount() {
	            // TODO Auto-generated method stub
	            return groupArray.size();
	        }
	 
	        @Override
	        public long getGroupId(int groupPosition) {
	            // TODO Auto-generated method stub
	            return groupPosition;
	        }
	 
	        @Override
	        public View getGroupView(int groupPosition, boolean isExpanded,
	                View convertView, ViewGroup parent) {
	             
	           String   string=groupArray.get(groupPosition);
	           return getGenericView(string);
	        }
	 
	        @Override
	        public boolean hasStableIds() {
	            // TODO Auto-generated method stub
	            return false;
	        }
	 
	        @Override
	        public boolean isChildSelectable(int groupPosition, int childPosition) 
	        {
	            // TODO Auto-generated method stub
	            return true;
	        }
	         
	        private TextView  getGenericView(String string ) 
	        {
	              AbsListView.LayoutParams  layoutParams =new AbsListView.LayoutParams(
	                    ViewGroup.LayoutParams.MATCH_PARENT,
	                    ViewGroup.LayoutParams.WRAP_CONTENT);
	               
	              TextView  textView =new TextView(activity);
	              textView.setLayoutParams(layoutParams);
	               
	              textView.setGravity(Gravity.CENTER_VERTICAL |Gravity.LEFT);
	               
	              textView.setPadding(40, 0, 0, 0);
	              textView.setText(string);
	              return textView;
	         }
	    }



 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved