码迷,mamicode.com
首页 > 移动开发 > 详细

android ExpandableListView详解

时间:2015-04-19 17:58:06      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:

点击显示展开项,先看效果:

技术分享技术分享

开始,1.先搞个XML显示主界面:

main_activity.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
  >
	<ExpandableListView
        android:id ="@+id/expandableListView"  
        android:layout_width ="match_parent"  
        android:layout_height ="wrap_content"  
     />
</LinearLayout>

2.进入main_activity开始搞显示工作:

//定义两个List用来控制Group和Child中的String;
	
	private  List<String>  groupArray;//组列表
    private  List<List<String>> 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<String>  childItem =new ArrayList<String>();
        for(int index=0;index<child.length;index++)
        {
            childItem.add(child[index]);
        }
         childArray.add(childItem);
	}


初始化之后,可进行适配器适配:

 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;
	         }
	    }



android ExpandableListView详解

标签:

原文地址:http://blog.csdn.net/howlaa/article/details/45129523

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!