今天在公司搞了一个多级列表的东东,正好用在实际项目中很不错哦!废话不多说直接上源码。。。
1,创建一个显示的布局文件(expandablelistview.xml),我喜欢叫它UI视图容器。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ExpandableListView
android:id="@+id/test"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:childDivider="#ffffff"
android:cacheColorHint="@android:color/transparent"
android:scrollbars="none"
android:background="@drawable/ll_status_selector"
>
</ExpandableListView>
</RelativeLayout>
2,在ACtivity中获取ID,然后添加到Adapter中。
//数据源
ArrayList<String> groupList = new ArrayList<String>();
for (int i = 0; i < 3; i++) {
groupList.add("视频" + i + 1);
}
ArrayList<String> itemList1 = new ArrayList<String>();
itemList1.add("试题1");
itemList1.add("试题2");
ArrayList<String> itemList2 = new ArrayList<String>();
itemList2.add("试题1");
itemList2.add("试题2");
itemList2.add("试题3");
ArrayList<String> itemList3 = new ArrayList<String>();
itemList3.add("试题1");
itemList3.add("试题2");
itemList3.add("试题3");
itemList3.add("试题4");
ArrayList<ArrayList<String>> childList = new ArrayList<ArrayList<String>>();
childList.add(itemList1);
childList.add(itemList2);
childList.add(itemList3);
//获取ID
ExpandableListView layout_lv = (ExpandableListView) content_layout1.findViewById(R.id.test);
//创建Adapter(自定义Adapter在下面介绍)
PracticeAdapter testAdapter = new PracticeAdapter( groupList, childList);
layout_lv.setAdapter(testAdapter);
layout_lv.setCacheColorHint(0x00000000);
layout_lv.setSelector(new ColorDrawable(Color.TRANSPARENT));
layout_lv.setGroupIndicator(null);
3,接下来就是最重要的Adapter了,我也喜欢叫它装载和现实的容器。自己好好去参悟一下去吧。
private class PracticeAdapter extends BaseExpandableListAdapter {
private ArrayList<String> groupList;
private ArrayList<ArrayList<String>> childList;
PracticeAdapter (ArrayList<String> groupList, ArrayList<ArrayList<String>> childList) {
this.groupList = groupList;
this.childList = childList;
}
public Object getChild(int groupPosition, int childPosition) {
return childList.get(groupPosition).get(childPosition);
}
private int selectedGroupPosition = -1;
private int selectedChildPosition = -1;
public void setSelectedPosition(int selectedGroupPosition, int selectedChildPosition) {
this.selectedGroupPosition = selectedGroupPosition;
this.selectedChildPosition = selectedChildPosition;
}
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
public int getChildrenCount(int groupPosition) {
return childList.get(groupPosition).size();
}
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
TextView textView = null;
if (convertView == null) {
textView = new TextView(MainActivity.this);
textView.setPadding(32, 10, 0, 10);
convertView = textView;
} else {
textView = (TextView) convertView;
}
textView.setText(getChild(groupPosition, childPosition).toString());
if (groupPosition == selectedGroupPosition) {
if (childPosition == selectedChildPosition) {
textView.setBackgroundColor(0xffb6ddee);
} else {
textView.setBackgroundColor(Color.TRANSPARENT);
}
}
textView.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
setSelectedPosition(groupPosition, childPosition);
notifyDataSetChanged();
}
});
return textView;
}
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) {
LinearLayout cotain = new LinearLayout(MainActivity.this);
cotain.setPadding(0, 10, 0, 10);
cotain.setGravity(Gravity.CENTER_VERTICAL);
ImageView imgIndicator = new ImageView(MainActivity.this);
TextView textView = new TextView(MainActivity.this);
textView.setText(getGroup(groupPosition).toString());
textView.setPadding(5, 0, 0, 0);
if (isExpanded) {
imgIndicator.setBackgroundResource(R.drawable.ic_launcher);
} else {
imgIndicator.setBackgroundResource(R.drawable.ic_launcher);
}
cotain.addView(imgIndicator);
cotain.addView(textView);
return cotain;
}
public boolean hasStableIds() {
return true;
}
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
最后说下几个特殊的方法:
1,默认展开列表
for (int i = 0; i < testAdapter.getGroupCount(); i++) {
layout_lv.expandGroup(i);
}
2,点击一级列表是否收缩return false(收起),return true(不收起)
layout_lv.setOnGroupClickListener(new OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) { //
Toast.makeText(CourseDetailActivity.this, "点击了标题" + groupPosition ,
Toast.LENGTH_SHORT).show();
return true;
}
});
3,点击二级列表的监听
layout_lv.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
Toast.makeText(CourseDetailActivity.this, "点击了标题" + groupPosition + "下的二层列表定位" + childPosition, Toast.LENGTH_SHORT).show();
return true;
}
});
同志们OK大家可以去做实验了,没准还会有意外的收获哦(希望大家能看得懂我写东东)。。。