入门篇ExpandableListAdapter(简易树形组件)

 package org.lxh.demo;

/*实现简单的树形组件

*可以以我的代码为参考进行优化得到更好看的

*/

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;


public class MyExpandableListAdapter extends BaseExpandableListAdapter {
// 一个组下肯定会有多个子选项
private String[] groups = new String[] { "我的好友", "家人", "同事", "黑名单" }; // 组名称
private String[][] children = new String[][] {
{ "李兴华", "张三", "李四", "MLDN" }, { "父亲", "母亲" },
{ "王五", "赵六", "王八" }, { "票贩子", "造假商" } }; // 这里面定义的子选项
private Context context = null ;
public MyExpandableListAdapter(Context context) {
this.context = context ;
}
@Override
public Object getChild(int groupPosition, int childPosition) { // 取得指定的子选项
return this.children[groupPosition][childPosition];
}


@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}



private TextView buildTextView() {
AbsListView.LayoutParams params = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT, 35);
TextView textView = new TextView(this.context) ;
textView.setLayoutParams(params) ;
textView.setTextSize(15.0f) ;
textView.setGravity(Gravity.LEFT) ;
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 groupPosition, int childPosition) {
return true;
}


}
package org.lxh.demo;


import android.app.Activity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.Toast;


public class MyExpandableListViewDemo extends Activity {
private ExpandableListView elistview = null;
private ExpandableListAdapter adapter = null;


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main);
this.elistview = (ExpandableListView) super
.findViewById(R.id.elistview);
this.adapter = new MyExpandableListAdapter(this);
this.elistview.setAdapter(this.adapter);


super.registerForContextMenu(this.elistview);


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(
MyExpandableListViewDemo.this,
"子选项被选中,groupPosition = " + groupPosition
+ ",childPosition = " + childPosition,
Toast.LENGTH_SHORT).show();
return false;
}
}


private class OnGroupClickListenerImpl implements OnGroupClickListener {


@Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
Toast.makeText(MyExpandableListViewDemo.this,
"分组被选中,groupPosition = " + groupPosition,
Toast.LENGTH_SHORT).show();
return false;
}


}


private class OnGroupExpandListenerImpl implements OnGroupExpandListener {


@Override
public void onGroupExpand(int groupPosition) {
Toast.makeText(MyExpandableListViewDemo.this,
"打开分组,groupPosition = " + groupPosition, Toast.LENGTH_SHORT)
.show();


}


}


private class OnGroupCollapseListenerImpl implements
OnGroupCollapseListener {


@Override
public void onGroupCollapse(int groupPosition) {
Toast.makeText(MyExpandableListViewDemo.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(MyExpandableListViewDemo.this,
"type = " + type + ",group = " + group + ",child = " + child,
Toast.LENGTH_SHORT).show();
}



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<ExpandableListView
android:id="@+id/elistview"  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    />
</LinearLayout>

你可能感兴趣的:(android)