当前位置:操作系统 > 安卓/Android >>

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

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

 \

 

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
<ExpandableListView  
    android:id="@+id/list"  
    android:layout_width="fill_parent"  
    android:layout_height="wrap_content"  
    android:childIndicator="@drawable/icon"  
    ></ExpandableListView> 
</LinearLayout> 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 >
<ExpandableListView
 android:id="@+id/list"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:childIndicator="@drawable/icon"
 ></ExpandableListView>
</LinearLayout>


public class ExpandableListViewTest extends Activity 

    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
        //创建一个BaseExpandableListAdapter对象  
        ExpandableListAdapter adapter = new BaseExpandableListAdapter() 
        { 
            int[] logos = new int[] 
            { 
                R.drawable.sunyz_1,  
                R.drawable.sunyz_6, 
                R.drawable.sunyz_7 
            }; 
            private String[] CDs = new String[] 
                { "风筝", "完美的一天", "是时候"}; 
            private String[][] songs = new String[][] 
            {  
                { "绿光", "不是真的爱我", "爱情字典", "练习" }, 
                { "Honey Honey", "心愿", "明天晴天", "隐形人" }, 
                { "愚人的国度", "是时候" , "世说心语" } 
            }; 
            //获取指定组位置、指定子列表项处的子列表项数据  
            @Override 
            public Object getChild(int groupPosition, int childPosition) 
            { 
                return songs[groupPosition][childPosition]; 
            } 
            @Override 
            public long getChildId(int groupPosition, int childPosition) 
            { 
                return childPosition; 
            } 
            @Override 
            public int getChildrenCount(int groupPosition) 
            { 
                return songs[groupPosition].length; 
            } 
            private TextView getTextView() 
            { 
                //用于实现条目的虚拟列表的基类. 这里的列表没有空间的定义。 例如,该类的子类可以以网格的形式、走马灯的形式显示,或者作为堆栈等等  
                AbsListView.LayoutParams lp = new AbsListView.LayoutParams( 
                        ViewGroup.LayoutParams.FILL_PARENT, 64);  //设置宽和高   
                TextView textView = new TextView(ExpandableListViewTest.this); 
                textView.setLayoutParams(lp); 
                textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT); 
                textView.setPadding(36, 0, 0, 0); 
                textView.setTextSize(20); 
                return textView; 
            } 
            //该方法决定每个子选项的外观  
            @Override 
  &nbs

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,