Anroid类似QQ好友分组悬浮Demo_联系人PinnedHeaderListView源代码
程序员文章站
2022-03-01 13:33:38
...
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