expandlistview的DEMO,来自网络。
for (int i = 0; i < 5; i++) {
Map<String, String> curGroupMap = new HashMap<String, String>();
groupData.add(curGroupMap);
curGroupMap.put(G_TEXT, "Group " + i);
List<Map<String, String>> children = new ArrayList<Map<String, String>>();
for (int j = 0; j < 5; j++) {
Map<String, String> curChildMap = new HashMap<String, String>();
children.add(curChildMap);
curChildMap.put(C_TEXT1, "Child " + j);
curChildMap.put(C_TEXT2, "Child " + j);
}
childData.add(children);
}
adapter = new ExAdapter(ExListView.this);
exList = (ExpandableListView) findViewById(R.id.list);
exList.setAdapter(adapter);
exList.setGroupIndicator(null);
exList.setDivider(null);
exList.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
Log.d("test", "onChildClick" + "childPosition" + childPosition);
Log.d("test", "onChildClick" + "groupPosition" + groupPosition);
return false;
}
});
exList.setOnGroupExpandListener(new OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
Log.d("test", "onGroupExpand");
Log.d("test", "onGroupExpand" + "groupPosition" + groupPosition );
}
});
class ExAdapter extends BaseExpandableListAdapter {
ExListView exlistview;
public ExAdapter(ExListView elv) {
super();
exlistview = elv;
}
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.member_listview, null);
}
TextView title = (TextView) view.findViewById(R.id.content_001);
title.setText(getGroup(groupPosition).toString());
ImageView image = (ImageView) view.findViewById(R.id.tubiao);
if (isExpanded)
image.setBackgroundResource(R.drawable.btn_browser2);
else
image.setBackgroundResource(R.drawable.btn_browser);
return view;
}
public long getGroupId(int groupPosition) {
return groupPosition;
}
public Object getGroup(int groupPosition) {
return groupData.get(groupPosition).get(G_TEXT).toString();
}
public int getGroupCount() {
return groupData.size();
}
// **************************************
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.member_childitem, null);
}
final TextView title = (TextView) view
.findViewById(R.id.child_text);
title.setText(childData.get(groupPosition).get(childPosition).get(
C_TEXT1).toString());
final TextView title2 = (TextView) view
.findViewById(R.id.child_text2);
title2.setText(childData.get(groupPosition).get(childPosition).get(
C_TEXT2).toString());
if(childData.get(groupPosition).size() == 1){
}else if(childData.get(groupPosition).size() > 1){
if(childData.get(groupPosition).size() % 2 == 0){ //偶数
if(childPosition == 0){//white up
Log.d("test", "偶数white up");
}else if(childPosition == childData.get(groupPosition).size() - 1){ //black down
Log.d("test", "偶数black down");
}else if((childPosition + 1) % 2 != 0){//white nomal
Log.d("test", "偶数white nomal");
}else if((childPosition + 1) % 2 == 0){//black normal
Log.d("test", "偶数black normal");
}
}else if(childData.get(groupPosition).size() % 2 != 0){
if(childPosition == 0){//white up
Log.d("test", groupPosition + "奇数white up" +childPosition);
}else if(childPosition == childData.get(groupPosition).size() - 1){ //white down
Log.d("test", groupPosition + "奇数white down"+childPosition);
}else if((childPosition + 1) % 2 != 0){//white nomal
Log.d("test", groupPosition + "奇数white nomal"+childPosition);
}else if((childPosition + 1) % 2 == 0){//black normal
Log.d("test", groupPosition + "奇数black normal"+childPosition);
}
}
}
return view;
}
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
public Object getChild(int groupPosition, int childPosition) {
return childData.get(groupPosition).get(childPosition).get(C_TEXT1)
.toString();
}
public int getChildrenCount(int groupPosition) {
return childData.get(groupPosition).size();
}
// **************************************
public boolean hasStableIds() {
return true;
}
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
转载:
http://www.linuxidc.com/Linux/2011-08/41349.htm
客户提出需求,要求分组列表单击一个group其他的group应该关闭,这个group的group标题应该顶头。
我网上大致找了一圈没找到相关的,于是去google group里搜索了下 ,发现有人提出了他的解决方法:
受此启发我想出了自己的方法,重写setOnGroupClickListener方法,使用flag控制:
定义flag记录列表状态
int expandFlag = -1;//控制列表的展开
重写onGroupClick方法
1.ExpandableListView listView = getExpandableListView();
2.
3.listView.setOnGroupClickListener(new OnGroupClickListener() {
4.
5. @Override
6. public boolean onGroupClick(ExpandableListView listView, View v,
7. int groupPosition, long id) {
8.
9. if (expandFlag == -1) {
2. //展开被选的group
3. listView.expandGroup(groupPosition);
4. //设置被选中的group置于顶端
5. listView.setSelectedGroup(groupPosition);
6. expandFlag = groupPosition;
2. } else if (expandFlag == groupPosition) { 3. listView.collapseGroup(expandFlag);
4. expandFlag = -1; 5. } else {
6. listView.collapseGroup(expandFlag);
7. //展开被选的group
8. listView.expandGroup(groupPosition);
9. //设置被选中的group置于顶端
10. listView.setSelectedGroup(groupPosition);
11. expandFlag = groupPosition; 12. }
13.
14. return true;
15. }
16.});
本篇文章来源于 Linux公社网站(www.linuxidc.com) 原文链接:http://www.linuxidc.com/Linux/2011-08/41349.htm
自己修改的:
mApplicationsList.setOnGroupClickListener(new OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView listView, View v,int groupPosition, long id) {
TGLog.d("__groupPosition__expandFlag_" + groupPosition + "|" + expandFlag);
if(groupPosition == 0){
behDB.addOneBehCount("1806", 3);
}else if(groupPosition == 1){
behDB.addOneBehCount("1801", 3);
}
//实现:分组列表单击一个group其他的group应该关闭,这个group的group标题应该顶头。
if (expandFlag == -1) {
// 展开被选的group
if(mApplicationsList.isGroupExpanded(groupPosition)){
listView.collapseGroup(groupPosition);
}else {
if(groupPosition == 1){
listView.collapseGroup(0);
listView.expandGroup(groupPosition);
}else if(groupPosition == 0){
listView.expandGroup(groupPosition);
listView.collapseGroup(1);
}
}
// 设置被选中的group置于顶端
listView.setSelectedGroup(groupPosition);
expandFlag = groupPosition;
} else if (expandFlag == groupPosition) {
if(mApplicationsList.isGroupExpanded(groupPosition)){
listView.collapseGroup(expandFlag);
}else {
if(groupPosition == 1){
listView.collapseGroup(0);
listView.expandGroup(groupPosition);
}else if(groupPosition == 0){
listView.expandGroup(groupPosition);
listView.collapseGroup(1);
}
}
expandFlag = -1;
} else {
if(mApplicationsList.isGroupExpanded(groupPosition)){
listView.collapseGroup(expandFlag);
}else {
// 展开被选的group
if(groupPosition == 1){
listView.collapseGroup(0);
listView.expandGroup(groupPosition);
}else if(groupPosition == 0){
listView.expandGroup(groupPosition);
listView.collapseGroup(1);
}
}
// 设置被选中的group置于顶端
listView.setSelectedGroup(groupPosition);
expandFlag = groupPosition;
}
return true;
}
});
转载:
http://k-beta.com/android-learning-expandablelistview-contact.html
ExpandableListView listView = getExpandableListView();
listView.setOnGroupClickListener(new OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView listView, View v,
int groupPosition, long id) {
if (expandFlag == -1) {
//展开被选的group
listView.expandGroup(groupPosition);
//设置被选中的group置于顶端
listView.setSelectedGroup(groupPosition);
expandFlag = groupPosition;
} else if (expandFlag == groupPosition) {
listView.collapseGroup(expandFlag);
expandFlag = -1;
} else {
listView.collapseGroup(expandFlag);
//展开被选的group
listView.expandGroup(groupPosition);
//设置被选中的group置于顶端
listView.setSelectedGroup(groupPosition);
expandFlag = groupPosition;
}
return true;
}
});
转载: