歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Android開發教程:擴展的ListView實例

從網上看了個擴展的Android listView的例子,感覺還蠻炫,自己也試著做了下。

由於比較簡單,所有就直接上代碼:         

[html]
  1. public class ExtendedListView extends ExpandableListActivity {  
  2.   
  3.     @Override  
  4.     protected void onCreate(Bundle savedInstanceState) {  
  5.         // TODO Auto-generated method stub  
  6.         super.onCreate(savedInstanceState);  
  7.           
  8.         MyExpandableListAdapter adapter=new MyExpandableListAdapter();  
  9.         setListAdapter(adapter);  
  10.           
  11.     }  
  12.     public class MyExpandableListAdapter extends BaseExpandableListAdapter{  
  13.         public String[] groups={"我的好友","大學同學","高中同學"};  
  14.         public String[][] childrens={{"小張","小李","小麗","向明"},{"向明","向明","向明","向明"},{"向明","向明","向明","向明"}};  
  15.         public Object getChild(int groupPosition, int childPosition) {  
  16.             // TODO Auto-generated method stub  
  17.             return childrens[groupPosition][childPosition];  
  18.         }  
  19.   
  20.         public long getChildId(int groupPosition, int childPosition) {  
  21.             // TODO Auto-generated method stub  
  22.             return childPosition;  
  23.         }  
  24.   
  25.         public View getChildView(int groupPosition, int childPosition,  
  26.                 boolean isLastChild, View convertView, ViewGroup parent) {  
  27.             // TODO Auto-generated method stub  
  28.             TextView textView=getGenericView();  
  29.             textView.setText(getChild(groupPosition, childPosition).toString());  
  30.             return textView;  
  31.         }  
  32.         //新建一個TextView  
  33.          public TextView getGenericView() {  
  34.                 // Layout parameters for the ExpandableListView  
  35.                 AbsListView.LayoutParams lp = new AbsListView.LayoutParams(  
  36.                         ViewGroup.LayoutParams.MATCH_PARENT, 64);  
  37.   
  38.                 TextView textView = new TextView(ExtendedListView.this);  
  39.                 textView.setLayoutParams(lp);  
  40.                 // Center the text vertically  
  41.                 textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);  
  42.                 // Set the text starting position  
  43.                 textView.setPadding(36, 0, 0, 0);  
  44.                 return textView;  
  45.             }  
  46.               
  47.         public int getChildrenCount(int groupPosition) {  
  48.             // TODO Auto-generated method stub  
  49.             return childrens[groupPosition].length;  
  50.         }  
  51.   
  52.         public Object getGroup(int groupPosition) {  
  53.             // TODO Auto-generated method stub  
  54.             return groups[groupPosition];  
  55.         }  
  56.   
  57.         public int getGroupCount() {  
  58.             // TODO Auto-generated method stub  
  59.             return groups.length;  
  60.         }  
  61.   
  62.         public long getGroupId(int groupPosition) {  
  63.             // TODO Auto-generated method stub  
  64.             return groupPosition;  
  65.         }  
  66.   
  67.         public View getGroupView(int groupPosition, boolean isExpanded,  
  68.                 View convertView, ViewGroup parent) {  
  69.             // TODO Auto-generated method stub  
  70.             TextView textView = getGenericView();  
  71.             textView.setText(getGroup(groupPosition).toString());  
  72.             return textView;  
  73.         }  
  74.   
  75.         public boolean hasStableIds() {  
  76.             // TODO Auto-generated method stub  
  77.             return true;  
  78.         }  
  79.   
  80.         public boolean isChildSelectable(int groupPosition, int childPosition) {  
  81.             // TODO Auto-generated method stub  
  82.             return true;  
  83.         }  
  84.           
  85.     }  
  86. }  
看一下運行在模擬器上的效果:   

Copyright © Linux教程網 All Rights Reserved