扩展的ListView实例

来源:互联网 发布:淘宝女生船袜 编辑:程序博客网 时间:2024/04/29 02:54

今天从网上看了个扩展的listView的例子,感觉还蛮炫,自己也试着做了下。

           由于比较简单,所有就直接上代码:

         

[html] view plain copy
  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. }  

      看一下运行在模拟器上的效果:

     



0 0