android學習筆記22:可展開的ListView

來源:互聯網
上載者:User

有時在寫程式時,我們希望一個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 
            public View getChildView(int groupPosition, int childPosition, 
                    boolean isLastChild, View convertView, ViewGroup parent) 
            { 
                TextView textView = getTextView();           
                textView.setText(getChild(groupPosition, childPosition).toString()); 
                return textView; 
            } 
            //擷取指定組位置處的組資料  
            @Override 
            public Object getGroup(int groupPosition) 
            { 
                return CDs[groupPosition]; 
            } 
            @Override 
            public int getGroupCount() 
            { 
                return CDs.length; 
            } 
            @Override 
            public long getGroupId(int groupPosition) 
            { 
                return groupPosition; 
            } 
            //該方法決定每個組選項的外觀  
            @Override 
            public View getGroupView(int groupPosition, boolean isExpanded, 
                    View convertView, ViewGroup parent) 
            { 
                LinearLayout ll = new LinearLayout(ExpandableListViewTest.this); 
                ll.setOrientation(0); 
                ImageView logo = new ImageView(ExpandableListViewTest.this); 
                logo.setImageResource(logos[groupPosition]); 
                ll.addView(logo); 
                TextView textView = getTextView(); 
                textView.setText(getGroup(groupPosition).toString());                
                ll.addView(textView);            
                return ll; 
            } 
            @Override 
            public boolean isChildSelectable(int groupPosition, int childPosition) 
            { 
                return true; 
            } 
            @Override 
            public boolean hasStableIds() 
            { 
                return true; 
            } 
        }; 
        ExpandableListView expandListView = (ExpandableListView) 
            findViewById(R.id.list); 
        expandListView.setAdapter(adapter); 
    } 


摘自 snoopy的專欄

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.