树形ExpandableListView控件使用

来源:互联网 发布:阿里云短信服务 编辑:程序博客网 时间:2024/05/03 12:16

原文:http://blog.csdn.net/ly_rose/article/details/50358433

还有一篇比较完整:http://blog.csdn.net/sysukehan/article/details/51960473

创建工程Android_expandableListView中添加控件ExpandableListView

[html] view plain copy
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:paddingBottom="@dimen/activity_vertical_margin"  
  6.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  7.     android:paddingRight="@dimen/activity_horizontal_margin"  
  8.     android:paddingTop="@dimen/activity_vertical_margin"  
  9.     tools:context="com.example.android_expandablelistview.MainActivity" >  
  10.   
  11.     <ExpandableListView  
  12.         android:id="@+id/expandableListView1"  
  13.         android:layout_width="match_parent"  
  14.         android:layout_height="wrap_content"  
  15.         android:layout_alignParentLeft="true"  
  16.         android:layout_alignParentTop="true" >  
  17.     </ExpandableListView>  
  18.   
  19. </RelativeLayout>  

[java] view plain copy
  1. package com.example.android_expandablelistview;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import android.support.v7.app.ActionBarActivity;  
  7. import android.os.Bundle;  
  8. import android.view.Menu;  
  9. import android.view.MenuItem;  
  10. import android.view.View;  
  11. import android.view.ViewGroup;  
  12. import android.widget.BaseExpandableListAdapter;  
  13. import android.widget.ExpandableListView;  
  14. import android.widget.TextView;  
  15.   
  16. public class MainActivity extends ActionBarActivity {  
  17.   
  18.     private ExpandableListView listView;  
  19.     private MyAdapter adapter;  
  20.     private List<String> group;//组  
  21.     private List<List<String>> child;//子  
  22.       
  23.       
  24.     @Override  
  25.     protected void onCreate(Bundle savedInstanceState) {  
  26.         super.onCreate(savedInstanceState);  
  27.         setContentView(R.layout.activity_main);  
  28.         listView=(ExpandableListView) this.findViewById(R.id.expandableListView1);  
  29.           
  30.         adapter=new MyAdapter();  
  31.         initData();  
  32.         listView.setAdapter(adapter);  
  33.           
  34.         listView.setGroupIndicator(null);//把箭头去掉  
  35.           
  36.     }  
  37.       
  38.     //初始化数据,组和子元素实例化  
  39.     public void initData(){  
  40.         group=new ArrayList<String>();  
  41.         child=new ArrayList<List<String>>();  
  42.           
  43.         addInfo("广东",new String[]{"深圳","珠海","广州"});  
  44.         addInfo("湖北",new String[]{"武汉","孝感","黄冈"});  
  45.         addInfo("河南",new String[]{"郑州","洛阳","商丘"});  
  46.           
  47.     }  
  48.       
  49.     public void addInfo(String g,String[] c){  
  50.         group.add(g);//添加组  
  51.         List<String> list = new ArrayList<String>();  
  52.         for(int i=0;i<c.length;i++){  
  53.             list.add(c[i]);  
  54.         }  
  55.         child.add(list);  
  56.     }  
  57.       
  58.      class MyAdapter extends BaseExpandableListAdapter{  
  59.   
  60.             @Override  
  61.             public int getGroupCount() {  
  62.                 // TODO Auto-generated method stub  
  63.                 return group.size();  
  64.             }  
  65.   
  66.             @Override  
  67.             public int getChildrenCount(int groupPosition) {  
  68.                 // TODO Auto-generated method stub  
  69.                 return child.size();  
  70.             }  
  71.   
  72.             @Override  
  73.             public Object getGroup(int groupPosition) {  
  74.                 // TODO Auto-generated method stub  
  75.                 return group.get(groupPosition);  
  76.             }  
  77.   
  78.             @Override  
  79.             public Object getChild(int groupPosition, int childPosition) {  
  80.                 // TODO Auto-generated method stub  
  81.                 return child.get(groupPosition).get(childPosition);  
  82.             }  
  83.   
  84.             @Override  
  85.             public long getGroupId(int groupPosition) {  
  86.                 // TODO Auto-generated method stub  
  87.                 return groupPosition;  
  88.             }  
  89.   
  90.             @Override  
  91.             public long getChildId(int groupPosition, int childPosition) {  
  92.                 // TODO Auto-generated method stub  
  93.                 return childPosition;  
  94.             }  
  95.   
  96.             @Override  
  97.             public boolean hasStableIds() {  
  98.                 // TODO Auto-generated method stub  
  99.                 return false;  
  100.             }  
  101.   
  102.             @Override  
  103.             public View getGroupView(int groupPosition, boolean isExpanded,  
  104.                     View convertView, ViewGroup parent) {  
  105.                 // TODO Auto-generated method stub  
  106.                   
  107.                 TextView textView = null;  
  108.                 if(convertView==null){  
  109.                     textView = new TextView(MainActivity.this);  
  110.                 }else{  
  111.                     textView = (TextView)convertView;  
  112.                 }  
  113.                 textView.setText(group.get(groupPosition));  
  114.                 textView.setTextSize(30);  
  115.                 textView.setPadding(3610010);  
  116.                 return textView;  
  117.             }  
  118.   
  119.             @Override  
  120.             public View getChildView(int groupPosition, int childPosition,  
  121.                     boolean isLastChild, View convertView, ViewGroup parent) {  
  122.                 // TODO Auto-generated method stub  
  123.                 TextView textView = null;  
  124.                 if(convertView==null){  
  125.                     textView = new TextView(MainActivity.this);  
  126.                 }else{  
  127.                     textView = (TextView)convertView;  
  128.                 }  
  129.                 textView.setText(child.get(groupPosition).get(childPosition));  
  130.                 textView.setTextSize(20);  
  131.                 textView.setPadding(7210010);  
  132.                 return textView;  
  133.             }  
  134.   
  135.             @Override  
  136.             public boolean isChildSelectable(int groupPosition, int childPosition) {  
  137.                 // TODO Auto-generated method stub  
  138.                 return true;  
  139.             }  
  140.               
  141.         }  
  142.           
  143.   
  144.     @Override  
  145.     public boolean onCreateOptionsMenu(Menu menu) {  
  146.         // Inflate the menu; this adds items to the action bar if it is present.  
  147.         getMenuInflater().inflate(R.menu.main, menu);  
  148.         return true;  
  149.     }  
  150.   
  151.     @Override  
  152.     public boolean onOptionsItemSelected(MenuItem item) {  
  153.         // Handle action bar item clicks here. The action bar will  
  154.         // automatically handle clicks on the Home/Up button, so long  
  155.         // as you specify a parent activity in AndroidManifest.xml.  
  156.         int id = item.getItemId();  
  157.         if (id == R.id.action_settings) {  
  158.             return true;  
  159.         }  
  160.         return super.onOptionsItemSelected(item);  
  161.     }  
  162. }  

运行效果图如下

去掉箭头,加上

[java] view plain copy
  1. listView.setGroupIndicator(null);//把箭头去掉  

0 0
原创粉丝点击