源码:
http://download.youkuaiyun.com/detail/sinat_31057219/9735884
参考:Android中ExpandableListView的使用(一)
ExpandableListView的使用demo : Ablexq/MyHelpCenter
知识点:
ExpandableListView的使用
public class ExpandableListView extends ListView {...}
效果图:
布局:
<ExpandableListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="10dp"
android:childDivider="#c8c7cc"
android:divider="@null"
android:dividerHeight="1px"
android:gravity="center"
android:groupIndicator="@null"
android:listSelector="#00000000"
android:scrollbars="none" />
代码
设置适配器:一次传递主目录和子目录的数据源。
adapter = new ExpandableListAdapter(this, list, child_text_array);
expandableListView.setAdapter(adapter);
点击事件:
expandableListView.setOnGroupClickListener(new OnGroupClickListener() {...}
expandableListView.setOnChildClickListener(new OnChildClickListener() {...}
详细如下:
public class ExpandableListViewActivity extends Activity {
private ExpandableListView expandableListView;
private ExpandableListAdapter adapter;
private List<Map<String, Object>> list;
private String[][] child_text_array;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_expandable_listview);
init();
initModle();
setListener();
}
private void init() {
expandableListView = (ExpandableListView) findViewById(R.id.list);
child_text_array = Model.EXPANDABLE_MORELIST_TXT;
}
private void setListener() {
expandableListView.setOnGroupClickListener(new OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
return false;
}
});
expandableListView.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
Toast.makeText(getApplicationContext(),
child_text_array[groupPosition][childPosition],
Toast.LENGTH_SHORT).show();
return false;
}
});
}
private void initModle() {
list = new ArrayList<Map<String, Object>>();
for (int i = 0; i < Model.EXPANDABLE_LISTVIEW_TXT.length; i++) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("img", Model.EXPANDABLE_LISTVIEW_IMG[i]);
map.put("txt", Model.EXPANDABLE_LISTVIEW_TXT[i]);
list.add(map);
}
adapter = new ExpandableListAdapter(this, list, child_text_array);
expandableListView.setAdapter(adapter);
}
}
adapter:
继承自BaseExpandableListAdapter,
实现
getGroupCount、getChildrenCount、
getGroup、getChild、
getGroupId、getChildId、
getGroupView、getChildView
等方法。
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private String[][] child_text_array;
private Context context;
private List<Map<String, Object>> list;
public ExpandableListAdapter(Context context,
List<Map<String, Object>> list, String[][] child_text_array) {
this.context = context;
this.list = list;
this.child_text_array = child_text_array;
}
/**
* 获取一级标签总数
*/
@Override
public int getGroupCount() {
return list.size();
}
/**
* 获取一级标签下二级标签的总数
*/
@Override
public int getChildrenCount(int groupPosition) {
return child_text_array[groupPosition].length;
}
/**
* 获取一级标签内容
*/
@Override
public Object getGroup(int groupPosition) {
return list.get(groupPosition).get("txt");
}
/**
* 获取一级标签下二级标签的内容
*/
@Override
public Object getChild(int groupPosition, int childPosition) {
return child_text_array[groupPosition][childPosition];
}
/**
* 获取一级标签的ID
*/
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
/**
* 获取二级标签的ID
*/
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
/**
* 指定位置相应的组视图
*/
@Override
public boolean hasStableIds() {
return true;
}
/**
* 对一级标签进行设置
*/
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
convertView = (LinearLayout) LinearLayout.inflate(context,
R.layout.item_group_layout, null);
ImageView group_icon = (ImageView) convertView
.findViewById(R.id.img_icon);
TextView group_title = (TextView) convertView
.findViewById(R.id.group_title);
if (isExpanded) {
group_title.setCompoundDrawablesWithIntrinsicBounds(0, 0,
R.drawable.group_down, 0);//设置是否打开的标志
} else {
group_title.setCompoundDrawablesWithIntrinsicBounds(0, 0,
R.drawable.group_up, 0);//textView的drawableRight
}
group_icon.setImageResource(Integer.parseInt(list.get(groupPosition)
.get("img").toString()));
group_title.setText(list.get(groupPosition).get("txt").toString());
return convertView;
}
/**
* 对一级标签下的二级标签进行设置
*/
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
convertView = (RelativeLayout) RelativeLayout.inflate(context,
R.layout.item_child_layout, null);
TextView child_text = (TextView) convertView
.findViewById(R.id.child_text);
child_text.setText(child_text_array[groupPosition][childPosition]);
return convertView;
}
/**
* 当选择子节点的时候,调用该方法
*/
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
ExpandableListView只打开一个组,关闭其他组:
当我们在使用ExpandableListView的时候,打开一个组的时候,要关闭其他组;需要用到setOnGroupExpandListener来进行设置:
mExpandListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
int count = mExpandListView.getExpandableListAdapter().getGroupCount();
for(int j = 0; j < count; j++){
if(j != groupPosition){
mExpandListView.collapseGroup(j);
}
}
}
});