Android中ExpandableListView的使用

來源:互聯網
上載者:User


ExpandableListView是android中可以實現下拉list的一個控制項,具體的實現方法如下:

首先:在layout的xml檔案中定義一個ExpandableListView

 

  1. <LinearLayout   
  2.     android:id="@+id/linearLayout"  
  3.     android:layout_width="fill_parent"   
  4.     android:layout_height="fill_parent"  
  5.     androidrientation="vertical"  
  6.     >  
  7.       
  8.     <ExpandableListView  
  9.     android:id="@+id/expandableListView"  
  10.     android:layout_width="fill_parent"  
  11.     android:layout_height="wrap_content"  
  12.         />  
  13. </LinearLayout>  

 

 

定義兩個List,用來存放控制項中Group/Child中的String

 

  1. private List<String> groupArray;  
  2. private List<List<String>> childArray;  

 

 

對這兩個List進行初始化,並插入一些資料

 

  1. groupArray = new ArrayList<String>();  
  2. childArray = new ArrayList<List<String>>();  
  3.   
  4. groupArray.add("第一行");  
  5. groupArray.add("第二行");  
  6.   
  7. List<String> tempArray = new ArrayList<String>();  
  8. tempArray.add("第一條");  
  9. tempArray.add("第二條");  
  10. tempArray.add("第三條");  
  11.   
  12. for(int index = 0; index <groupArray.size(); ++index)  
  13. {  
  14.     childArray.add(tempArray);  
  15. }  

 

定義ExpandableListView的Adapter

 

  1. //ExpandableListView的Adapter  
  2. public class ExpandableAdapter extends BaseExpandableListAdapter  
  3. {  
  4.     Activity activity;  
  5.       
  6.     public ExpandableAdapter(Activity a)  
  7.     {  
  8.         activity = a;  
  9.     }  
  10.     public Object getChild(int groupPosition, int childPosition)  
  11.     {  
  12.         return childArray.get(groupPosition).get(childPosition);  
  13.     }  
  14.     public long getChildId(int groupPosition, int childPosition)  
  15.     {  
  16.         return childPosition;  
  17.     }  
  18.     public int getChildrenCount(int groupPosition)  
  19.     {  
  20.         return childArray.get(groupPosition).size();  
  21.     }  
  22.     public View getChildView(int groupPosition, int childPosition,  
  23.             boolean isLastChild, View convertView, ViewGroup parent)  
  24.     {  
  25.         String string = childArray.get(groupPosition).get(childPosition);  
  26.         return getGenericView(string);  
  27.     }  
  28.     // group method stub  
  29.     public Object getGroup(int groupPosition)  
  30.     {  
  31.         return groupArray.get(groupPosition);  
  32.     }  
  33.     public int getGroupCount()  
  34.     {  
  35.         return groupArray.size();  
  36.     }  
  37.     public long getGroupId(int groupPosition)  
  38.     {  
  39.         return groupPosition;  
  40.     }  
  41.     public View getGroupView(int groupPosition, boolean isExpanded,  
  42.             View convertView, ViewGroup parent)  
  43.     {  
  44.         String string = groupArray.get(groupPosition);  
  45.         return getGenericView(string);  
  46.     }  
  47.     // View stub to create Group/Children 's View  
  48.     public TextView getGenericView(String string)  
  49.     {  
  50.         // Layout parameters for the ExpandableListView  
  51.         AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams(  
  52.                 ViewGroup.LayoutParams.FILL_PARENT, 64);  
  53.         TextView text = new TextView(activity);  
  54.         text.setLayoutParams(layoutParams);  
  55.         // Center the text vertically  
  56.         text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);  
  57.         // Set the text starting position  
  58.         text.setPadding(36, 0, 0, 0);  
  59.         text.setText(string);  
  60.         return text;  
  61.     }  
  62.     public boolean hasStableIds()  
  63.     {  
  64.         return false;  
  65.     }  
  66.     public boolean isChildSelectable(int groupPosition, int childPosition)  
  67.     {  
  68.         return true;  
  69.     }  
  70. }  

 

最後,給定義好的ExpandableListView添加上Adapter

 

  1. ExpandableListView expandableListView = (ExpandableListView)findViewById(R.id.expandableListView);  
  2. expandableListView.setAdapter(new ExpandableAdapter(Main.this));  

 

運行即可見效果~~~

 

 

 

 

 

 

 

Android版手風琴(ExpandableListView)

先看效果,過癮一番。

 

 

 源碼下載:http://files.cnblogs.com/salam/WidgetDemo.rar

 

  

  ExpandableListView是Android中的手風琴,本人感覺效果相當棒。

  一、ExpandableListView介紹

    一個垂直滾動的顯示兩個層級(Child,Group)清單項目的視圖,清單項目來自ExpandableListAdapter 。組可以單獨展開。

  1.重要方法

      expandGroup(int groupPos) :在分組列表視圖中展開一組,

      setSelectedGroup(int groupPosition) :設定選擇指定的組。

      setSelectedChild(int groupPosition, int childPosition, boolean shouldExpandGroup) :設定選擇指定的子項。

      getPackedPositionGroup(long packedPosition) :返回所選擇的組

      getPackedPositionForChild(int groupPosition, int childPosition) :返回所選擇的子項

      getPackedPositionType(long packedPosition) :返回所選擇項的類型(Child,Group)

      isGroupExpanded(int groupPosition) :判斷此組是否展開

  2.代碼:

ExpandableListContextMenuInfo menuInfo=(ExpandableListContextMenuInfo)item.getMenuInfo();
  String title=((TextView)menuInfo.targetView).getText().toString();
  int type=ExpandableListView.getPackedPositionType(menuInfo.packedPosition);
  
  if (type==ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
  int groupPos =ExpandableListView.getPackedPositionGroup(menuInfo.packedPosition);
  int childPos =ExpandableListView.getPackedPositionChild(menuInfo.packedPosition);

二、ExpandableListAdapter

    一個介面,將基礎資料連結到一個ExpandableListView。此介面的實施將提供訪問Child的資料(由組分類),並執行個體化的Child和Group。

  1.重要方法

    getChildId(int groupPosition, int childPosition) 擷取與在給定組給予孩子相關的資料。

    getChildrenCount(int groupPosition) 返回在指定Group的Child數目。

  2.代碼

 public class MyExpandableListAdapter extends BaseExpandableListAdapter {
         // Sample data set.  children[i] contains the children (String[]) for groups[i].
         public String[] groups = { "我的好友", "新疆同學", "親戚", "同事" };
         public String[][] children = {
                 { "胡算林", "張俊峰", "王志軍", "二人" },
                 { "李秀婷", "蔡喬", "別高", "餘音" },
                 { "攤派新", "張愛明" },
                 { "馬超", "司道光" }
         };
         
         public Object getChild(int groupPosition, int childPosition) {
             return children[groupPosition][childPosition];
         }

         public long getChildId(int groupPosition, int childPosition) {
             return childPosition;
         }

         public int getChildrenCount(int groupPosition) {
             return children[groupPosition].length;
         }

         public TextView getGenericView() {
             // Layout parameters for the ExpandableListView
             AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
                     ViewGroup.LayoutParams.MATCH_PARENT, 64);

             TextView textView = new TextView(ExpandableListDemo.this);
             textView.setLayoutParams(lp);
             // Center the text vertically
             textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
             // Set the text starting position
             textView.setPadding(36, 0, 0, 0);
             return textView;
         }
         
         public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
                 View convertView, ViewGroup parent) {
             TextView textView = getGenericView();
             textView.setText(getChild(groupPosition, childPosition).toString());
             return textView;
         }

         public Object getGroup(int groupPosition) {
             return groups[groupPosition];
         }

         public int getGroupCount() {
             return groups.length;
         }

         public long getGroupId(int groupPosition) {
             return groupPosition;
         }

         public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
                 ViewGroup parent) {
             TextView textView = getGenericView();
             textView.setText(getGroup(groupPosition).toString());
             return textView;
         }

         public boolean isChildSelectable(int groupPosition, int childPosition) {
             return true;
         }

         public boolean hasStableIds() {
             return true;
         }

     }

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.