android学习笔记22:可展开的ListView

来源:互联网 发布:表结构列的删除的sql 编辑:程序博客网 时间:2024/06/08 07:47

http://blog.csdn.net/hn307165411/article/details/7173284

有时在写程序时,我们希望一个listview能展开其下的子类目,在android中可以通过使用ExpandAbleListView来实现,只需要在代码里为ExpandAbleListView设置一个ExpandAbleLIstAdapter的数据源即可。

[html] view plaincopyprint?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7. <ExpandableListView   
  8.     android:id="@+id/list"   
  9.     android:layout_width="fill_parent"   
  10.     android:layout_height="wrap_content"   
  11.     android:childIndicator="@drawable/icon"   
  12.     ></ExpandableListView>  
  13. </LinearLayout>  

[java] view plaincopyprint?
  1. public class ExpandableListViewTest extends Activity  
  2. {  
  3.     @Override  
  4.     public void onCreate(Bundle savedInstanceState)  
  5.     {  
  6.         super.onCreate(savedInstanceState);  
  7.         setContentView(R.layout.main);  
  8.         //创建一个BaseExpandableListAdapter对象  
  9.         ExpandableListAdapter adapter = new BaseExpandableListAdapter()  
  10.         {  
  11.             int[] logos = new int[]  
  12.             {  
  13.                 R.drawable.sunyz_1,   
  14.                 R.drawable.sunyz_6,  
  15.                 R.drawable.sunyz_7  
  16.             };  
  17.             private String[] CDs = new String[]  
  18.                 { "风筝""完美的一天""是时候"};  
  19.             private String[][] songs = new String[][]  
  20.             {   
  21.                 { "绿光""不是真的爱我""爱情字典""练习" },  
  22.                 { "Honey Honey""心愿""明天晴天""隐形人" },  
  23.                 { "愚人的国度""是时候" , "世说心语" }  
  24.             };  
  25.             //获取指定组位置、指定子列表项处的子列表项数据  
  26.             @Override  
  27.             public Object getChild(int groupPosition, int childPosition)  
  28.             {  
  29.                 return songs[groupPosition][childPosition];  
  30.             }  
  31.             @Override  
  32.             public long getChildId(int groupPosition, int childPosition)  
  33.             {  
  34.                 return childPosition;  
  35.             }  
  36.             @Override  
  37.             public int getChildrenCount(int groupPosition)  
  38.             {  
  39.                 return songs[groupPosition].length;  
  40.             }  
  41.             private TextView getTextView()  
  42.             {  
  43.                 //用于实现条目的虚拟列表的基类. 这里的列表没有空间的定义。 例如,该类的子类可以以网格的形式、走马灯的形式显示,或者作为堆栈等等  
  44.                 AbsListView.LayoutParams lp = new AbsListView.LayoutParams(  
  45.                         ViewGroup.LayoutParams.FILL_PARENT, 64);  //设置宽和高   
  46.                 TextView textView = new TextView(ExpandableListViewTest.this);  
  47.                 textView.setLayoutParams(lp);  
  48.                 textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);  
  49.                 textView.setPadding(36000);  
  50.                 textView.setTextSize(20);  
  51.                 return textView;  
  52.             }  
  53.             //该方法决定每个子选项的外观  
  54.             @Override  
  55.             public View getChildView(int groupPosition, int childPosition,  
  56.                     boolean isLastChild, View convertView, ViewGroup parent)  
  57.             {  
  58.                 TextView textView = getTextView();            
  59.                 textView.setText(getChild(groupPosition, childPosition).toString());  
  60.                 return textView;  
  61.             }  
  62.             //获取指定组位置处的组数据  
  63.             @Override  
  64.             public Object getGroup(int groupPosition)  
  65.             {  
  66.                 return CDs[groupPosition];  
  67.             }  
  68.             @Override  
  69.             public int getGroupCount()  
  70.             {  
  71.                 return CDs.length;  
  72.             }  
  73.             @Override  
  74.             public long getGroupId(int groupPosition)  
  75.             {  
  76.                 return groupPosition;  
  77.             }  
  78.             //该方法决定每个组选项的外观  
  79.             @Override  
  80.             public View getGroupView(int groupPosition, boolean isExpanded,  
  81.                     View convertView, ViewGroup parent)  
  82.             {  
  83.                 LinearLayout ll = new LinearLayout(ExpandableListViewTest.this);  
  84.                 ll.setOrientation(0);  
  85.                 ImageView logo = new ImageView(ExpandableListViewTest.this);  
  86.                 logo.setImageResource(logos[groupPosition]);  
  87.                 ll.addView(logo);  
  88.                 TextView textView = getTextView();  
  89.                 textView.setText(getGroup(groupPosition).toString());                 
  90.                 ll.addView(textView);             
  91.                 return ll;  
  92.             }  
  93.             @Override  
  94.             public boolean isChildSelectable(int groupPosition, int childPosition)  
  95.             {  
  96.                 return true;  
  97.             }  
  98.             @Override  
  99.             public boolean hasStableIds()  
  100.             {  
  101.                 return true;  
  102.             }  
  103.         };  
  104.         ExpandableListView expandListView = (ExpandableListView)  
  105.             findViewById(R.id.list);  
  106.         expandListView.setAdapter(adapter);  
  107.     }  
  108. }  

0 0
原创粉丝点击