Anroid类似QQ好友分组悬浮Demo_联系人PinnedHeaderListView源代码
2015-08-21 12:44:06 By: shinyuu
Android中、大家都用过ListView、ExpandableListView等、也许你还用过PinnedHeaderListView、但是如果我说PinnedHeaderExpandableListView、你听过吗?还有可下拉的PinnedHeaderExpandableListView呢?
没听过也不要紧、本文就是介绍这个东西的、为了让大家有更直观的了解、先上效果图、通过效果图可以看出、首先它是一个ExpandableListView、但是它的头部可以固定、其次、在它的上面还有一个头部可以来回伸缩、恩、这就是本文要介绍的自定义view、为了提高复用性、这个效果我分成来了2个view来实现
第一个是PinnedHeaderExpandableListView来实现头部固定的ExpandableListView、第二个view是StickyLayout、这个view具有一个可以上下滑动的头部、最后将这2个view组合在一起、就达到了如下的效果
onCreate()代码
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); expandableListView = (PinnedHeaderExpandableListView) findViewById(R.id.expandablelist); //stickyLayout = (StickyLayout)findViewById(R.id.sticky_layout); initData(); adapter = new MyexpandableListAdapter(this); expandableListView.setAdapter(adapter); // 展开所有group for (int i = 0, count = expandableListView.getCount(); i < count; i ) { expandableListView.expandGroup(i); } expandableListView.setOnHeaderUpdateListener(this); expandableListView.setOnChildClickListener(this); expandableListView.setOnGroupClickListener(this); //stickyLayout.setOnGiveUpTouchEventListener(this); }
getGroupView()代码
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { GroupHolder groupHolder = null; if (convertView == null) { groupHolder = new GroupHolder(); convertView = inflater.inflate(R.layout.group, null); groupHolder.textView = (TextView) convertView .findViewById(R.id.group); groupHolder.imageView = (ImageView) convertView .findViewById(R.id.image); convertView.setTag(groupHolder); } else { groupHolder = (GroupHolder) convertView.getTag(); } groupHolder.textView.setText(((Group) getGroup(groupPosition)) .getTitle()); if (isExpanded)// ture is Expanded or false is not isExpanded groupHolder.imageView.setImageResource(R.drawable.expanded); else groupHolder.imageView.setImageResource(R.drawable.collapse); return convertView; }
getChildView()代码
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { ChildHolder childHolder = null; if (convertView == null) { childHolder = new ChildHolder(); convertView = inflater.inflate(R.layout.child, null); childHolder.textName = (TextView) convertView .findViewById(R.id.name); childHolder.textAge = (TextView) convertView .findViewById(R.id.age); childHolder.textAddress = (TextView) convertView .findViewById(R.id.address); childHolder.imageView = (ImageView) convertView .findViewById(R.id.image); Button button = (Button) convertView .findViewById(R.id.button1); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this, "clicked pos=", Toast.LENGTH_SHORT).show(); } }); convertView.setTag(childHolder); } else { childHolder = (ChildHolder) convertView.getTag(); } childHolder.textName.setText(((People) getChild(groupPosition, childPosition)).getName()); childHolder.textAge.setText(String.valueOf(((People) getChild( groupPosition, childPosition)).getAge())); childHolder.textAddress.setText(((People) getChild(groupPosition, childPosition)).getAddress()); return convertView; }
最后给贴上Demo的源代码、希望对大家有用、有兴趣的哥们可以下载看看
源代码下载链接: http://dwtedx.com/download.html?bdkey=s/1dDua5Rr 密码: j3d6
若资源对你有帮助、浏览后有很大收获、不妨小额打赏我一下、你的鼓励是维持我不断写博客最大动力
想获取DD博客最新代码、你可以扫描下方的二维码、关注DD博客微信公众号(ddblogs)
或者你也可以关注我的新浪微博、了解DD博客的最新动态:DD博客官方微博(dwtedx的微博)
如对资源有任何疑问或觉得仍然有很大的改善空间、可以对该博文进行评论、希望不吝赐教
为保证及时回复、可以使用博客留言板给我留言: DD博客留言板(dwtedx的留言板)
感谢你的访问、祝你生活愉快、工作顺心、欢迎常来逛逛