你好,游客 登录 注册 搜索
背景:
阅读新闻

Android之带group指示器的ExpandableListView

[日期:2013-05-14] 来源:Linux社区  作者:weidi1989 [字体:大 中 小]

其中最重要的是下面这部分:我们监听onSroll这个接口事件,当ListView在滑动时,做相应的处理,代码中注释比较多,我这里就不多作说明了。

  1. /**
  2. * here is very importance for indicator group
  3. */
  4. publicvoid onScroll(AbsListView view, int firstVisibleItem,
  5. int visibleItemCount, int totalItemCount) {
  6. final ExpandableListView listView = (ExpandableListView) view;
  7. /**
  8. * calculate point (0,0)
  9. */
  10. int npos = view.pointToPosition(0, 0);// 其实就是firstVisibleItem
  11. if (npos == AdapterView.INVALID_POSITION)// 如果第一个位置值无效
  12. return;
  13. long pos = listView.getExpandableListPosition(npos);
  14. int childPos = ExpandableListView.getPackedPositionChild(pos);// 获取第一行child的id
  15. int groupPos = ExpandableListView.getPackedPositionGroup(pos);// 获取第一行group的id
  16. if (childPos == AdapterView.INVALID_POSITION) {// 第一行不是显示child,就是group,此时没必要显示指示器
  17. View groupView = listView.getChildAt(npos
  18. - listView.getFirstVisiblePosition());// 第一行的view
  19. indicatorGroupHeight = groupView.getHeight();// 获取group的高度
  20. indicatorGroup.setVisibility(View.GONE);// 隐藏指示器
  21. } else {
  22. indicatorGroup.setVisibility(View.VISIBLE);// 滚动到第一行是child,就显示指示器
  23. }
  24. // get an error data, so return now
  25. if (indicatorGroupHeight == 0) {
  26. return;
  27. }
  28. // update the data of indicator group view
  29. if (groupPos != indicatorGroupId) {// 如果指示器显示的不是当前group
  30. mAdapter.getGroupView(groupPos, listView.isGroupExpanded(groupPos),
  31. indicatorGroup.getChildAt(0), null);// 将指示器更新为当前group
  32. indicatorGroupId = groupPos;
  33. Log.e(TAG, PRE + "bind to new group,group position = " + groupPos);
  34. // mAdapter.hideGroup(indicatorGroupId); // we set this group view
  35. // to be hided
  36. // 为此指示器增加点击事件
  37. indicatorGroup.setOnClickListener(new OnClickListener() {
  38. publicvoid onClick(View v) {
  39. // TODO Auto-generated method stub
  40. listView.collapseGroup(indicatorGroupId);
  41. }
  42. });
  43. }
  44. if (indicatorGroupId == -1) // 如果此时grop的id无效,则返回
  45. return;
  46. /**
  47. * calculate point (0,indicatorGroupHeight) 下面是形成往上推出的效果
  48. */
  49. int showHeight = indicatorGroupHeight;
  50. int nEndPos = listView.pointToPosition(0, indicatorGroupHeight);// 第二个item的位置
  51. if (nEndPos == AdapterView.INVALID_POSITION)// 如果无效直接返回
  52. return;
  53. long pos2 = listView.getExpandableListPosition(nEndPos);
  54. int groupPos2 = ExpandableListView.getPackedPositionGroup(pos2);// 获取第二个group的id
  55. if (groupPos2 != indicatorGroupId) {// 如果不等于指示器当前的group
  56. View viewNext = listView.getChildAt(nEndPos
  57. - listView.getFirstVisiblePosition());
  58. showHeight = viewNext.getTop();
  59. Log.e(TAG, PRE + "update the show part height of indicator group:"
  60. + showHeight);
  61. }
  62. // update group position
  63. MarginLayoutParams layoutParams = (MarginLayoutParams) indicatorGroup
  64. .getLayoutParams();
  65. layoutParams.topMargin = -(indicatorGroupHeight - showHeight);
  66. indicatorGroup.setLayoutParams(layoutParams);
  67. }

本文源码下载地址:

免费下载地址在 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

linux
相关资讯       Android group  ExpandableListView 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款