posts - 33, comments - 0, trackbacks - 0, articles - 0
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          Android 自定義 ExpandableListView

          Posted on 2011-10-06 09:37 馬航 閱讀(5469) 評論(0)  編輯  收藏

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


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 松潘县| 隆化县| 孟州市| 长沙市| 水富县| 鞍山市| 隆化县| 陆川县| 汾阳市| 腾冲县| 股票| 浙江省| 临泽县| 疏附县| 纳雍县| 石台县| 光泽县| 鞍山市| 汽车| 合山市| 京山县| 六安市| 赞皇县| 万州区| 诸暨市| 个旧市| 开封县| 博野县| 清新县| 樟树市| 道孚县| 河曲县| 饶平县| 石屏县| 台南县| 宁德市| 恩平市| 子长县| 浦县| 凤翔县| 循化|