Android 自定义 ExpandableListView

Android中有一控件是ExpandableListView,比ListView更高级,ExpandableListView的效果很实用,比如因为需要查看一堆文件的目录结构或者开发像QQ好友那样的界面,就应该使用Expandablelistview。

本文最终效果如下:

首先是Activity代码,实际开发中数据(包括父item,子item及图片,Expandablelistview布局也可以轻易更改)可以很方便的从数据库或网络动态取得,本文方便起见数据就先定死了。

public class C_ExpandableListView extends Activity {

    ExpandableListView expandableList
    public String[] str1 = { "我的好友", "陌生人", "黑名单","yilee","good","哈哈哈" }; 
    public String[] str2 = { "我了个去", "哈哈", "蟹", "XX", "我去" }; 
    public String[] str3 =  { "哈哈", "蟹", "XX", "我去" }; 
    public String[] str4 =  { "我了个去", "蟹", "XX", "我去" }; 
    public String[] str5 =  { "我了个去", "蟹", "XX", "我去" }; 
    public String[] str6 = { "yilee", "哈哈", "蟹", "XX", "我去" }; 
    public String[] str7 =  {  "哈哈", "蟹", "XX", "我去" }; 
    @Override 
    public void onCreate(Bundle savedInstanceState{ 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
        expandableList = (ExpandableListViewC_ExpandableListView.this 
                .findViewById(R.id.ExpandableListView01); 
        expandableList.setAdapter(new TreeViewAdapter(this)); 
        
    }

    public class TreeViewAdapter extends BaseExpandableListAdapter { 
        private LayoutInflater inflater
        private LayoutInflater inflater1;

        public TreeViewAdapter(Context c{ 
            this.inflater = LayoutInflater.from(c); 
            this.inflater1 = LayoutInflater.from(c); 
        }

        @Override 
        public Object getChild(int groupPosition, int childPosition{ 
            return childPosition
        }

        @Override 
        public long getChildId(int groupPosition, int childPosition{ 
            return 0
        }

        @Override 
        public View getChildView(int groupPosition, int childPosition, 
                boolean isLastChild, View convertView, ViewGroup parent{ 
            View myView = inflater1.inflate(R.layout.cc, null); 
            final int a=groupPosition
            final int b=childPosition
            myView.setBackgroundResource(R.drawable.child); 
            TextView textview = (TextViewmyView 
                    .findViewById(R.id.TextView001); 
            
            textview.setOnClickListener(new OnClickListener() { 
                
                @Override 
                public void onClick(View arg0{ 
                    Log.i("yileeeee", "groupPosition= "+a); 
                    Log.i("yileeeee", "childPosition= "+b); 
                } 
            });        
            if(groupPosition==0){ 
                textview.setText(str2[childPosition]); 
            }else if(groupPosition==1){ 
                textview.setText(str3[childPosition]); 
            }else if(groupPosition==2){ 
                textview.setText(str4[childPosition]);    
            }else if(groupPosition==3){ 
                textview.setText(str5[childPosition]);    
            }else if(groupPosition==4){ 
                textview.setText(str6[childPosition]);    
            }else if(groupPosition==5){ 
                textview.setText(str7[childPosition]);    
            }    
            return myView
        }

        @Override 
        public int getChildrenCount(int groupPosition{ 
            if(groupPosition==0){ 
                return str2.length
            }else if(groupPosition==1){ 
                return str3.length
            }else if(groupPosition==2){ 
                return str4.length
            }else if(groupPosition==3){ 
                return str5.length
            }else if(groupPosition==4){ 
                return str6.length
            }else { 
                return str7.length
            } 
        }

        @Override 
        public Object getGroup(int groupPosition{ 
            return "dd"
        }

        @Override 
        public int getGroupCount() { 
            return str1.length
        }

        @Override 
        public long getGroupId(int groupPosition{ 
            return groupPosition
        }

        @Override 
        public View getGroupView(int groupPosition, boolean isExpanded, 
                View convertView, ViewGroup parent{ 
            View myView = inflater.inflate(R.layout.dd, null);

            myView.setBackgroundResource(R.drawable.group); 
            TextView textview = (TextViewmyView.findViewById(R.id.TextView01); 
            textview.setText(str1[groupPosition]); 
            return myView
        }

        @Override 
        public boolean hasStableIds() { 
            return false
        }

        @Override 
        public boolean isChildSelectable(int groupPosition, int childPosition{ 
            return false
        } 
    } 
}

其中类TreeViewAdapter是我们的自定义Adapter,继承自BaseExpandableListAdapter。

getChildrenCount比较麻烦,因为么个子item数目并不一样,可以把子数据放入一个String三维数组,这样只需return str[position],返回的便是子item的数据,样在getChildView在也应该这样设置:textview.setText(str[position][childPosition]);   

我们需要三个布局文件,一个是activity里面是ExpandableListView

<ExpandableListView android:id="@+id/ExpandableListView01" 
        android:layout_width="200dp" android:layout_height="fill_parent" 
        android:layout_x="20dip" android:layout_y="30dip" 
        android:groupIndicator="@null"android:childDivider="@drawable/child_divider" 
        android:clickable="true" android:scrollbarAlwaysDrawHorizontalTrack="true"> 
</ExpandableListView>

第二个是描述父item的布局文件

<TextView android:id="@+id/TextView01" android:layout_width="wrap_content" 
        android:layout_height="wrap_content" android:textSize="16px" 
        android:gravity="center"> 
</TextView>

最后一个是描述子item的

<ImageView android:id="@+id/ImageView01" 
        android:layout_height="30dp" android:layout_width="30dp" 
        android:background="@drawable/head"> 
</ImageView>

<TextView android:id="@+id/TextView001" android:layout_height="fill_parent" 
        android:layout_width="fill_parent" android:gravity="center"> 
</TextView>

这样就可以轻易实现ExpandableListAdapter 高度自定义了。

posted @ 2011-10-06 09:37  mac的学习笔记  阅读(582)  评论(0编辑  收藏  举报