1、使用ExpandableListView可以完成数据的分组显示
2、ExpandableListView 组件的分组及子项都可以监听并进行相应处理
3、可以用上下文菜单的方法对子项菜单进行操作
此组件需要创建一个适配器组件填充数据,完成之后需要在组件上配置数据,而这个配置功能需要需要通过布局文件完成
适配器类继承了BaseExpandableListAdapter,对其方法进行重写 MyExpandableListViewAdapter.java
public class MyExpandableListAdapter extends BaseExpandableListAdapter {
private String[] groups = new String[] { "我的好友", "我的同事", "家人", "黑名单" };
private String[][] children = new String[][] { { "张三", "李四", "王五", "赵六" },
{ "田七", "沈八" }, { "哥哥", "姐姐" }, { "电话推销", "票贩子" } };
private Context context = null;
public MyExpandableListAdapter(Context context) {
this.context = context;
}
@Override
public Object getChild(int groupPosition, int childPosition) { //取得指定的子选项
return children[groupPosition][childPosition];
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
private TextView buildTextView(){
AbsListView.LayoutParams param=new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,100);
TextView textView=new TextView(this.context);
textView.setLayoutParams(param);
textView.setTextSize(15.0f);
textView.setGravity(Gravity.CENTER);
textView.setPadding(40, 8, 3, 3);
return textView;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
TextView textView=this.buildTextView();
textView.setText(this.getChild(groupPosition, childPosition).toString());
return textView;
}
@Override
public int getChildrenCount(int groupPosition) {
return this.children[groupPosition].length;
}
@Override
public Object getGroup(int groupPosition) {
return this.groups[groupPosition];
}
@Override
public int getGroupCount() {
return this.groups.length;
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
TextView textView=this.buildTextView();
textView.setText(this.getGroup(groupPosition).toString());
return textView;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int arg0, int arg1) {
return true;
}
}
主程序MyDemo.java 对组件进行展示
public class MyDemo extends Activity {
private ExpandableListView eListView = null;
private ExpandableListAdapter adapter = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main);
this.eListView = (ExpandableListView) super
.findViewById(R.id.eListView);
super.registerForContextMenu(this.eListView);
this.adapter = new MyExpandableListAdapter(this);
this.eListView.setAdapter(adapter);
this.eListView.setOnChildClickListener(new OnChildClickListenerImpl());
this.eListView.setOnGroupClickListener(new OnGroupClickListenerImpl());
this.eListView
.setOnGroupExpandListener(new OnGroupExpandListenerImpl());
this.eListView
.setOnGroupCollapseListener(new OnGroupCollapseListenerImpl());
}
private class OnChildClickListenerImpl implements OnChildClickListener {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
Toast.makeText(
MyDemo.this,
"子选项被选中,groupPosition=" + groupPosition + ",childPosition="
+ childPosition, Toast.LENGTH_SHORT).show();
return false;
}
}
private class OnGroupClickListenerImpl implements OnGroupClickListener {
@Override
public boolean onGroupClick(ExpandableListView arg0, View arg1,
int groupPosition, long arg3) {
Toast.makeText(MyDemo.this, "分组被选中,groupPosition=" + groupPosition,
Toast.LENGTH_SHORT).show();
return false;
}
}
private class OnGroupExpandListenerImpl implements OnGroupExpandListener {
@Override
public void onGroupExpand(int groupPosition) {
Toast.makeText(MyDemo.this, "打开分组,groupPosition=" + groupPosition,
Toast.LENGTH_SHORT).show();
}
}
private class OnGroupCollapseListenerImpl implements
OnGroupCollapseListener {
@Override
public void onGroupCollapse(int groupPosition) {
Toast.makeText(MyDemo.this, "关闭分组,groupPosition=" + groupPosition,
Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
ExpandableListView.ExpandableListContextMenuInfo info = (ExpandableListView.ExpandableListContextMenuInfo) menuInfo;
int type = ExpandableListView
.getPackedPositionType(info.packedPosition); // 取得菜单项
int group = ExpandableListView
.getPackedPositionGroup(info.packedPosition);// 取得所在组的索引
int child = ExpandableListView
.getPackedPositionChild(info.packedPosition);// 取得子菜单项的索引
Toast.makeText(MyDemo.this,
"type=" + type + ",group=" + group + ",child=" + child,
Toast.LENGTH_LONG).show();
}
}
private ExpandableListView eListView = null;
private ExpandableListAdapter adapter = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main);
this.eListView = (ExpandableListView) super
.findViewById(R.id.eListView);
super.registerForContextMenu(this.eListView);
this.adapter = new MyExpandableListAdapter(this);
this.eListView.setAdapter(adapter);
this.eListView.setOnChildClickListener(new OnChildClickListenerImpl());
this.eListView.setOnGroupClickListener(new OnGroupClickListenerImpl());
this.eListView
.setOnGroupExpandListener(new OnGroupExpandListenerImpl());
this.eListView
.setOnGroupCollapseListener(new OnGroupCollapseListenerImpl());
}
private class OnChildClickListenerImpl implements OnChildClickListener {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
Toast.makeText(
MyDemo.this,
"子选项被选中,groupPosition=" + groupPosition + ",childPosition="
+ childPosition, Toast.LENGTH_SHORT).show();
return false;
}
}
private class OnGroupClickListenerImpl implements OnGroupClickListener {
@Override
public boolean onGroupClick(ExpandableListView arg0, View arg1,
int groupPosition, long arg3) {
Toast.makeText(MyDemo.this, "分组被选中,groupPosition=" + groupPosition,
Toast.LENGTH_SHORT).show();
return false;
}
}
private class OnGroupExpandListenerImpl implements OnGroupExpandListener {
@Override
public void onGroupExpand(int groupPosition) {
Toast.makeText(MyDemo.this, "打开分组,groupPosition=" + groupPosition,
Toast.LENGTH_SHORT).show();
}
}
private class OnGroupCollapseListenerImpl implements
OnGroupCollapseListener {
@Override
public void onGroupCollapse(int groupPosition) {
Toast.makeText(MyDemo.this, "关闭分组,groupPosition=" + groupPosition,
Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
ExpandableListView.ExpandableListContextMenuInfo info = (ExpandableListView.ExpandableListContextMenuInfo) menuInfo;
int type = ExpandableListView
.getPackedPositionType(info.packedPosition); // 取得菜单项
int group = ExpandableListView
.getPackedPositionGroup(info.packedPosition);// 取得所在组的索引
int child = ExpandableListView
.getPackedPositionChild(info.packedPosition);// 取得子菜单项的索引
Toast.makeText(MyDemo.this,
"type=" + type + ",group=" + group + ",child=" + child,
Toast.LENGTH_LONG).show();
}
}
布局文件很简单 main.xml
<LinearLayout 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"
tools:context=".MyDemo" >
<ExpandableListView
android:id="@+id/eListView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
mn
</LinearLayout>
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MyDemo" >
<ExpandableListView
android:id="@+id/eListView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
mn
</LinearLayout>