package com.example.expandable; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.ExpandableListView; import android.widget.ExpandableListView.OnChildClickListener; import android.widget.ExpandableListView.OnGroupClickListener; import android.widget.Toast; public class MainActivity extends Activity { private ExpandableListView expandableListView; private List<String> groupList; private List<List<String>> childList; private MyAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); expandableListView = (ExpandableListView) findViewById(R.id.expandableListView); groupList = new ArrayList<String>(); childList = new ArrayList<List<String>>(); initDatas(); adapter = new MyAdapter(this, groupList, childList); expandableListView.setAdapter(adapter); expandableListView.setGroupIndicator(getResources().getDrawable(R.drawable.expand_select)); expandableListView.setOnGroupClickListener(new OnGroupClickListener() { @Override public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) { Toast.makeText(getApplicationContext(), groupList.get(groupPosition), Toast.LENGTH_SHORT).show(); return false; } }); expandableListView.setOnChildClickListener(new OnChildClickListener() { @Override public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { Toast.makeText(getApplicationContext(), childList.get(groupPosition).get(childPosition), Toast.LENGTH_SHORT).show(); return false; } }); } private void initDatas() { updateDatas("感興趣男明星", new String[] { "劉德華", "張學友", "郭富城" }); updateDatas("感興趣女明星", new String[] { "劉亦菲", "張馨予", "郭采潔" }); } private void updateDatas(String groupItem, String[] childs) { groupList.add(groupItem); List<String> childsItem = new ArrayList<String>(); for (String item : childs) { childsItem.add(item); } childList.add(childsItem); } }
package com.example.expandable; import java.util.List; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseExpandableListAdapter; import android.widget.TextView; public class MyAdapter extends BaseExpandableListAdapter { private Context context; private List<String> groupList; private List<List<String>> childList; private LayoutInflater layoutInflater; public MyAdapter(Context context, List<String> groupList, List<List<String>> childList) { this.context = context; this.groupList = groupList; this.childList = childList; this.layoutInflater = LayoutInflater.from(context); } @Override public int getGroupCount() { // TODO Auto-generated method stub return groupList.size(); } @Override public int getChildrenCount(int groupPosition) { return childList.get(groupPosition).size(); } @Override public Object getGroup(int groupPosition) { // TODO Auto-generated method stub return groupList.get(groupPosition); } @Override public Object getChild(int groupPosition, int childPosition) { // TODO Auto-generated method stub return childList.get(groupPosition).get(childPosition); } @Override public long getGroupId(int groupPosition) { // TODO Auto-generated method stub return groupPosition; } @Override public long getChildId(int groupPosition, int childPosition) { // TODO Auto-generated method stub return childPosition; } @Override public boolean hasStableIds() { return false; } @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { if (convertView==null) { convertView = layoutInflater.inflate(R.layout.group, parent,false); } TextView textView = (TextView) convertView.findViewById(R.id.tv_group); textView.setText(groupList.get(groupPosition)); return convertView; } @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { if (convertView==null) { convertView = layoutInflater.inflate(R.layout.child, null); } TextView textView = (TextView) convertView.findViewById(R.id.tv_child); textView.setText(childList.get(groupPosition).get(childPosition)); return convertView; } /** * 返回值爲true時 對child作出響應 */ @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } }