每天都在用QQ聊天,今天突然一想,android怎么實現列表的分組展開呢?看了看api,發現其實現過程也很簡單。先看一下最終效果吧!
1、首先創建我們的Activity,繼承 android.app.ExpandableListActivity,直接看代碼吧。
package com.ideasandroid.sample; import android.app.ExpandableListActivity; import android.os.Bundle; import com.ideasandroid.sample.adapter.IdeasExpandableListAdapter; /** * @author IdeasAndroid * 可展開(收縮)列表示例 */ public class IdeasExpandableListView extends ExpandableListActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //設置列表適配器IdeasExpandableListAdapter setListAdapter(new IdeasExpandableListAdapter(this)); } }
2、創建適配器,繼承android.widget.BaseExpandableListAdapter。
package com.ideasandroid.sample.adapter; import java.util.ArrayList; import java.util.List; import android.content.Context; import android.view.Gravity; import android.view.View; import android.view.ViewGroup; import android.widget.AbsListView; import android.widget.BaseExpandableListAdapter; import android.widget.TextView; /** * @author IdeasAndroid * 可展開(收縮)列表示例 */ public class IdeasExpandableListAdapter extends BaseExpandableListAdapter { private Context mContext = null; // 測試數據,開發時可能來自數據庫,網絡.... private String[] groups = { "家人", "朋友", "同事" }; private String[] familis = { "老爸", "老媽", "妹妹" }; private String[] friends = { "小李", "張三", "李四" }; private String[] colleagues = { "陳總", "李工", "李客戶" }; private List<String> groupList = null; private List<List<String>> itemList = null; public IdeasExpandableListAdapter(Context context) { this.mContext = context; groupList = new ArrayList<String>(); itemList = new ArrayList<List<String>>(); initData(); } /** * 初始化數據,將相關數據放到List中,方便處理 */ private void initData() { for (int i = 0; i < groups.length; i++) { groupList.add(groups[i]); } List<String> item1 = new ArrayList<String>(); for (int i = 0; i < familis.length; i++) { item1.add(familis[i]); } List<String> item2 = new ArrayList<String>(); for (int i = 0; i < friends.length; i++) { item2.add(friends[i]); } List<String> item3 = new ArrayList<String>(); for (int i = 0; i < colleagues.length; i++) { item3.add(colleagues[i]); } itemList.add(item1); itemList.add(item2); itemList.add(item3); } public boolean areAllItemsEnabled() { return false; } /* * 設置子節點對象,在事件處理時返回的對象,可存放一些數據 */ public Object getChild(int groupPosition, int childPosition) { return itemList.get(groupPosition).get(childPosition); } public long getChildId(int groupPosition, int childPosition) { return childPosition; } /* * 字節點視圖,這里我們顯示一個文本對象 */ public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { TextView text = null; if (convertView == null) { text = new TextView(mContext); } else { text = (TextView) convertView; } // 獲取子節點要顯示的名稱 String name = (String) itemList.get(groupPosition).get(childPosition); // 設置文本視圖的相關屬性 AbsListView.LayoutParams lp = new AbsListView.LayoutParams( ViewGroup.LayoutParams.FILL_PARENT, 40); text.setLayoutParams(lp); text.setTextSize(18); text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT); text.setPadding(45, 0, 0, 0); text.setText(name); return text; } /* * 返回當前分組的字節點個數 */ public int getChildrenCount(int groupPosition) { return itemList.get(groupPosition).size(); } /* * 返回分組對象,用於一些數據傳遞,在事件處理時可直接取得和分組相關的數據 */ public Object getGroup(int groupPosition) { return groupList.get(groupPosition); } /* * 分組的個數 */ public int getGroupCount() { return groupList.size(); } public long getGroupId(int groupPosition) { return groupPosition; } /* * 分組視圖,這里也是一個文本視圖 */ public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { TextView text = null; if (convertView == null) { text = new TextView(mContext); } else { text = (TextView) convertView; } String name = (String) groupList.get(groupPosition); AbsListView.LayoutParams lp = new AbsListView.LayoutParams( ViewGroup.LayoutParams.FILL_PARENT, 40); text.setLayoutParams(lp); text.setTextSize(18); text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT); text.setPadding(36, 0, 0, 0); text.setText(name); return text; } /* * 判斷分組是否為空,本示例中數據是固定的,所以不會為空,我們返回false * 如果數據來自數據庫,網絡時,可以把判斷邏輯寫到這個方法中,如果為空 * 時返回true */ public boolean isEmpty() { return false; } /* * 收縮列表時要處理的東西都放這兒 */ public void onGroupCollapsed(int groupPosition) { } /* * 展開列表時要處理的東西都放這兒 */ public void onGroupExpanded(int groupPosition) { } /* * Indicates whether the child and group IDs are stable across changes to * the underlying data. */ public boolean hasStableIds() { return false; } /* * Whether the child at the specified position is selectable. */ public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } }