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 高度自定義了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章