使用ExpandableListActivity實現可展開的Activity

package com.example.android_test;

import android.app.ExpandableListActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MyExpandableListActivity extends ExpandableListActivity{

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
			
		//創建適配器,使用BaseExpandableListAdapter
		ExpandableListAdapter adapter=new BaseExpandableListAdapter() {
			
			String[] listTitles={"列表標題1","列表標題2","列表標題3"};
			String[][] listContents={
					{"內容1","內容1","內容1","內容1","內容1"},
					{"內容2","內容2","內容2"},
					{"內容3","內容3","內容3","內容3"}
			};
			
			/**
			 * 是否選中指定位置上的子元素。
	         *  	參數
	         *         groupPosition 組位置(該組內部含有這個子元素)
	         *         childPosition  子元素位置
	         *  	返回值
	         *  	是否選中子元素
			 */
			public boolean isChildSelectable(int groupPosition, int childPosition) {
				return true;
			}
			
			@Override
			public int getGroupCount() {
				return listTitles.length;
			}

			@Override
			public int getChildrenCount(int groupPosition) {				
				return listContents[groupPosition].length;
			}
			/**
			 * 獲取指定組中的數據
			                  參數			
			         	groupPosition 組位置			
			                  返回值			
			                                  返回組中的數據,也就是該組中的子元素數據。
			 */
			@Override
			public Object getGroup(int groupPosition) {
				return listTitles[groupPosition];
			}

			/**
			 * 獲取指定組中的指定子元素數據。
             *     	返回值:返回指定子元素數據。
			 */
			@Override
			public Object getChild(int groupPosition, int childPosition) {				
				return listContents[groupPosition][childPosition];
			}

			/**
			 * 獲取指定組的ID,這個組ID必須是唯一的。聯合ID(參見getCombinedGroupId(long))在所有條目(所有組和所有元素)中也是唯一的。			
			                  參數			
			           groupPosition 組位置			
			                  返回值			
			                                返回組相關ID
			 */
			@Override
			public long getGroupId(int groupPosition) {
				return groupPosition;
			}

			/**
			 * 獲取指定組中的指定子元素ID,這個ID在組裏一定是唯一的。
			 *	                  參數				
             *              groupPosition    組位置(該組內部含有子元素)				
             *              childPosition    子元素位置(相對於其它子元素)				
			 *	                  返回值				
		     *              子元素關聯ID。
			 */
			@Override
			public long getChildId(int groupPosition, int childPosition) {
				return childPosition;
			}
			/**
			 *  組和子元素是否持有穩定的ID,也就是底層數據的改變不會影響到它們。
          				 返回值
   						 返回一個Boolean類型的值,如果爲TRUE,意味着相同的ID永遠引用相同的對象。
			 */
			@Override
			public boolean hasStableIds() {
				return true;
			}

			/**
			 * 決定每個組選項的外觀
			 * 獲取顯示指定組的視圖對象。這個方法僅返回關於組的視圖對象,要想獲取子元素的視圖對象,就需要調用getChildView(int, int, boolean, View, ViewGroup)。
			      參數			
					                   groupPosition 組位置(決定返回哪個視圖)			
					                   isExpanded    該組是展開狀態還是伸縮狀態			
					    	convertView  重用已有的視圖對象。注意:在使用前你應該檢查一下這個視圖對象
									              是否非空並且這個對象的類型是否合適。由此引伸出,如果該對象不能
									              被轉換並顯示正確的數據,這個方法就會調用
									     getGroupView(int, boolean, View, ViewGroup)
									            來創建一個視圖(View)對象。					
			                parent      返回的視圖對象始終依附於的視圖組。			
			           返回值			
			                                                返回指定組的視圖對象
			 */
			@Override
			public View getGroupView(int groupPosition, boolean isExpanded,
					View convertView, ViewGroup parent) {
				LinearLayout ll=new LinearLayout(MyExpandableListActivity.this);
				ll.setOrientation(0);
				TextView textView=getTextView();
				textView.setText(getGroup(groupPosition).toString());
				ll.addView(textView);
				return ll;
			}
			/**
			 * 獲取一個視圖對象,顯示指定組中的指定子元素數據。
			                    參數			
                        groupPosition  組位置(該組內部含有子元素)
                        childPosition   子元素位置(決定返回哪個視圖)
                        isLastChild    子元素是否處於組中的最後一個
		   			convertView   重用已有的視圖(View)對象。注意:在使用前你應該檢查一下這個視圖對象是否非空並且這個對象的類型是否合適。
									     由此引伸出,如果該對象不能被轉換並顯示正確的數據,
									     這個方法就會調用getChildView(int, int, boolean, View, ViewGroup)
									     來創建一個視圖(View)對象。			
		                parent       返回的視圖(View)對象始終依附於的視圖組。
			                  返回值			
                       	指定位置上的子元素返回的視圖對象
			 */
			@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;
			}
			
			//創建方法getTextView
			private TextView getTextView() {
				//AbsListView.LayoutParams.LayoutParams(int width, int height)
				//Abslistview 是用於實現條目的虛擬列表的基類.
				AbsListView.LayoutParams lp=new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 64);
				TextView textView=new TextView(MyExpandableListActivity.this);
				textView.setLayoutParams(lp);
				textView.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.LEFT);
				textView.setTextSize(20);
				textView.setPadding(60, 0, 0, 0);
				return textView;				
			}
		};
		setListAdapter(adapter);
	}
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章