ExpandableListView的使用

时间:2023-03-08 21:00:53

ExpandableListView的使用

效果图

ExpandableListView的使用

布局

<ExpandableListView
android:id="@+id/expandableListView"
android:layout_width="match_parent"
android:layout_height="match_parent" />

初始化

ExpandableListView expandableListView = (ExpandableListView) findViewById(R.id.expandableListView);

填充数据

KongqwExpandableListviewAdapter kongqwExpandableListviewAdapter = new KongqwExpandableListviewAdapter(this);
expandableListView.setAdapter(kongqwExpandableListviewAdapter);

Adapter

package com.example.kongqw.myapplication;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView; import java.util.ArrayList; /**
* Created by kongqw on 2015/12/21.
*/
public class KongqwExpandableListviewAdapter extends BaseExpandableListAdapter { private Context mContext;
private ArrayList<String> mGroups;
private ArrayList<String> mChilds; // 构造方法
public KongqwExpandableListviewAdapter(Context context) {
mContext = context;
// 模拟初始化数据
mGroups = new ArrayList<String>();
mGroups.add("Group 1");
mGroups.add("Group 2");
mGroups.add("Group 3");
mGroups.add("Group 4");
mGroups.add("Group 5");
mChilds = new ArrayList<String>();
mChilds.add("Child 1");
mChilds.add("Child 2");
mChilds.add("Child 3");
mChilds.add("Child 4");
mChilds.add("Child 5");
mChilds.add("Child 6");
mChilds.add("Child 7");
mChilds.add("Child 8");
mChilds.add("Child 9");
mChilds.add("Child 10");
} @Override
public int getGroupCount() {
return mGroups.size();
} @Override
public int getChildrenCount(int groupPosition) {
return mChilds.size();
} @Override
public Object getGroup(int groupPosition) {
return mGroups.get(groupPosition);
} @Override
public Object getChild(int groupPosition, int childPosition) {
return mChilds.get(childPosition);
} @Override
public long getGroupId(int groupPosition) {
return groupPosition;
} @Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
} @Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
View view = View.inflate(mContext, R.layout.expandable_group_item, null);
TextView textView = (TextView) view.findViewById(R.id.group_item);
textView.setText(mGroups.get(groupPosition));
return view;
} @Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
View view = View.inflate(mContext, R.layout.expandable_child_item, null);
TextView textView = (TextView) view.findViewById(R.id.child_item);
textView.setText(mChilds.get(childPosition));
return view;
} @Override
public boolean hasStableIds() {
return true;
} @Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}

去掉箭头

expandableListView.setGroupIndicator(null);

默认展开

// 设置ExpandableListView默认是展开的
for (int i = 0; i < kongqwExpandableListviewAdapter.getGroupCount(); i++) {
expandableListView.expandGroup(i);
}

Group不可点击

expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
// TODO Auto-generated method stub
return true;
}
});

TODO 复用