package com.sy.android.qqlistview;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.LinearLayout;
import android.widget.SimpleExpandableListAdapter;
import android.widget.TextView;
import com.sy.android.qqlistview.R;
;
public class QQlsit extends Activity implements OnGroupCollapseListener {
/** Called when the activity is first created. */
private static ArrayList<Map<String, String>> parentData = new ArrayList<Map<String, String>>();
private static ArrayList<ArrayList<Map<String, String>>> childData = new ArrayList<ArrayList<Map<String, String>>>();
private ExpandableListView elistview;
private TextView tv;
/**
*当前打开的父节点
*/
private int the_group_expand_position = -1;
/**
* 打开的父节点所与的子节点数
*/
private int position_child_count = 0;
/**
* 是否有打开的父节点
*/
private boolean isExpanding = false;
public void getData() {
for (int i = 0; i < 20; i++) {
Map<String, String> map = new HashMap<String, String>();
map.put("parend", i + "");
parentData.add(map);
}
for (int i = 0; i < 20; i++) {
ArrayList<Map<String, String>> child = new ArrayList<Map<String, String>>();
for (int j = 0; j < 20; j++) {
Map<String, String> map = new HashMap<String, String>();
map.put("child", i + "" + j);
child.add(map);
}
childData.add(child);
}
}
public void onCreate(Bundle saveBundle) {
super.onCreate(saveBundle);
setContentView(R.layout.main);
elistview = (ExpandableListView) findViewById(R.id.qq_listview);
// 替换ExpandableListView的打开关闭时的箭头图标
elistview.setGroupIndicator(this.getResources().getDrawable(
R.drawable.selector));
tv = (TextView) findViewById(R.id.qq_list_textview);
/**
* 滑动子列表时在上方显示父节点的view
*/
final LinearLayout linear = (LinearLayout) findViewById(R.id.gone_linear);
getData();
SimpleExpandableListAdapter selAdapter = new SimpleExpandableListAdapter(
this, parentData, R.layout.qq_list_parent,
new String[] { "parend" }, new int[] { R.id.parend },
childData, R.layout.qq_listview_child,
new String[] { "child" }, new int[] { R.id.child });
System.out.println(elistview);
elistview.setAdapter(selAdapter);
// linear.setOnClickListener(new OnClickListener() {
//
// @Override
// public void onClick(View v) {
// // TODO Auto-generated method stub
// int tmp = Integer.valueOf(tv.getText().toString());
// System.out.println("------"+"tmp :"+tmp);
// onGroupCollapse(tmp);
// }
// });
/**
* 监听父节点打开的事件
*/
elistview.setOnGroupExpandListener(new OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
the_group_expand_position = groupPosition;
position_child_count = childData.get(groupPosition).size();
isExpanding = true;
}
});
/**
* 监听父节点收缩的事件
*/
elistview.setOnGroupCollapseListener(this);
/**
* 通过setOnScrollListener来监听列表上下滑动时item显示和消失的事件
*/
elistview.setOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
if (isExpanding) {
// 当当前第一个item id小于打开的父节点id 或大于打开的父节点id和它的子节点总数之和时
if (firstVisibleItem < the_group_expand_position
|| firstVisibleItem > (the_group_expand_position + position_child_count)) {
linear.setVisibility(View.GONE);
} else {
linear.setVisibility(View.VISIBLE);
tv.setText(((Map) parentData
.get(the_group_expand_position)).get("parend")
.toString());
}
}
}
});
}
@Override
public void onGroupCollapse(int groupPosition) {
// TODO Auto-generated method stub
isExpanding = false;
the_group_expand_position = -1;
position_child_count = 0;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
前往页