其中最重要的是下面这部分:我们监听onSroll这个接口事件,当ListView在滑动时,做相应的处理,代码中注释比较多,我这里就不多作说明了。
- /**
- * here is very importance for indicator group
- */
- publicvoid onScroll(AbsListView view, int firstVisibleItem,
- int visibleItemCount, int totalItemCount) {
- final ExpandableListView listView = (ExpandableListView) view;
- /**
- * calculate point (0,0)
- */
- int npos = view.pointToPosition(0, 0);// 其实就是firstVisibleItem
- if (npos == AdapterView.INVALID_POSITION)// 如果第一个位置值无效
- return;
- long pos = listView.getExpandableListPosition(npos);
- int childPos = ExpandableListView.getPackedPositionChild(pos);// 获取第一行child的id
- int groupPos = ExpandableListView.getPackedPositionGroup(pos);// 获取第一行group的id
- if (childPos == AdapterView.INVALID_POSITION) {// 第一行不是显示child,就是group,此时没必要显示指示器
- View groupView = listView.getChildAt(npos
- - listView.getFirstVisiblePosition());// 第一行的view
- indicatorGroupHeight = groupView.getHeight();// 获取group的高度
- indicatorGroup.setVisibility(View.GONE);// 隐藏指示器
- } else {
- indicatorGroup.setVisibility(View.VISIBLE);// 滚动到第一行是child,就显示指示器
- }
- // get an error data, so return now
- if (indicatorGroupHeight == 0) {
- return;
- }
- // update the data of indicator group view
- if (groupPos != indicatorGroupId) {// 如果指示器显示的不是当前group
- mAdapter.getGroupView(groupPos, listView.isGroupExpanded(groupPos),
- indicatorGroup.getChildAt(0), null);// 将指示器更新为当前group
- indicatorGroupId = groupPos;
- Log.e(TAG, PRE + "bind to new group,group position = " + groupPos);
- // mAdapter.hideGroup(indicatorGroupId); // we set this group view
- // to be hided
- // 为此指示器增加点击事件
- indicatorGroup.setOnClickListener(new OnClickListener() {
- publicvoid onClick(View v) {
- // TODO Auto-generated method stub
- listView.collapseGroup(indicatorGroupId);
- }
- });
- }
- if (indicatorGroupId == -1) // 如果此时grop的id无效,则返回
- return;
- /**
- * calculate point (0,indicatorGroupHeight) 下面是形成往上推出的效果
- */
- int showHeight = indicatorGroupHeight;
- int nEndPos = listView.pointToPosition(0, indicatorGroupHeight);// 第二个item的位置
- if (nEndPos == AdapterView.INVALID_POSITION)// 如果无效直接返回
- return;
- long pos2 = listView.getExpandableListPosition(nEndPos);
- int groupPos2 = ExpandableListView.getPackedPositionGroup(pos2);// 获取第二个group的id
- if (groupPos2 != indicatorGroupId) {// 如果不等于指示器当前的group
- View viewNext = listView.getChildAt(nEndPos
- - listView.getFirstVisiblePosition());
- showHeight = viewNext.getTop();
- Log.e(TAG, PRE + "update the show part height of indicator group:"
- + showHeight);
- }
- // update group position
- MarginLayoutParams layoutParams = (MarginLayoutParams) indicatorGroup
- .getLayoutParams();
- layoutParams.topMargin = -(indicatorGroupHeight - showHeight);
- indicatorGroup.setLayoutParams(layoutParams);
- }
本文源码下载地址:
免费下载地址在 http://linux.linuxidc.com/
用户名与密码都是www.linuxidc.com
具体下载目录在 /2013年资料/5月/14日/Android之带group指示器的ExpandableListView
最后跟大家分享一下另外一个仿iPhone通讯录效果的代码。它是ListView的扩展,效果最好。希望有高手能把ExpandableListView扩展成一样的效果。 http://www.linuxidc.com/Linux/2013-05/84289.htm
更多Android相关信息见Android 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=11
