您可以使用 ExpandableListView 和 customAdapter
这里的activity实现了onChildClickListener onGroupCollapseListener和onGroupExpandListener
请注意,我只是直接输入代码,可能存在编译错误,请修复,如果有任何问题,请发表评论
public class YourActivity extends Activity implements OnChildClickListener, OnGroupCollapseListener{
private ExpandableListView exList= null;
private ExpandableListAdapter mAdapter = null;
//add all unimplemented methods
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
placeHolder = (LinearLayout) findViewById(R.id.placeHolder);
exList= (ExpandableListView) findViewById(R.id.wl_list);
exList.setHorizontalFadingEdgeEnabled(true);
exList.setFastScrollEnabled(true);
exList.setHorizontalFadingEdgeEnabled(true);
exList.setFadingEdgeLength(30);
exList.setDividerHeight(1);
exList.isFocusable();
exList.setTextFilterEnabled(true);
exList.setSelectionAfterHeaderView();
exList.setSelected(true);
exList.setOnChildClickListener(this);
mAdapter = new ExpandableListAdapter(this);
exList.setAdapter(mAdapter);
exList.setOnGroupCollapseListener(this);
exList.setOnGroupExpandListener(this);
setContentView(exList);
}
public void onGroupCollapse(int groupPosition) {
// do whatever additional
exList.collapseGroup(groupPosition);
}
public void onGroupExpand(int groupPosition) {
// do whatever additional
exList.expandGroup(groupPosition);
}
}
和一个自定义适配器,我只实现了 getChildView() 方法,请对 getGroupView() 方法执行相同的操作以完成
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Activity activity;
public ExpandableListAdapter(Activity activity) {
this.activity = activity;
}
public View getCustomChildView(int groupPosition, int childPosition) {
WORKLIST_HEADER wfHeader = null;
//View view = null;
//if (view == null) {
// you can do this also, if you prefer filling up screens via xml, that is, inflate
//view = inflater.inflate(R.layout.wl_list_row, null);
//}
//or jus create a linear layout manually and add views to it like below and return the linear layout, this should do
TextView textView = null;
LinearLayout wflayout = new LinearLayout(activity);
textView = new TextView(activity);
textView.setText("name");
textView.setTextAppearance(activity, R.style.boldText);
wflayout.addView(textView);
textView = new TextView(activity);
textView.setText("email_id");
textView.setTextAppearance(activity, R.style.boldText);
wflayout.addView(textView);
// add more and button as well,
//return linear layout
return wflayout;
}
public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
View convertView, ViewGroup parent) {
return getCustomChildView(groupPosition, childPosition);
}
// add otehr unimplemented methods
}