我们知道QQ好友列表分类使用ExpandableListView实现的,但是它在此基础上做改进,如打开分组,滑动时组标题会悬浮在最上端.
方便用户关闭分组.效果类似于下图:
实现代码如下所示:
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
//防止三星,魅族等手机第一个条目可以一直往下拉,父条目和悬浮同时出现的问题
if(firstVisibleItem==0){
view_flotage.setVisibility(View.GONE);
}
// 控制滑动时TextView的显示与隐藏
int npos = view.pointToPosition(0, 0);
if (npos != AdapterView.INVALID_POSITION) {
long pos = expandableList.getExpandableListPosition(npos);
int childPos = ExpandableListView.getPackedPositionChild(pos);
final int groupPos = ExpandableListView.getPackedPositionGroup(pos);
if (childPos == AdapterView.INVALID_POSITION) {
View groupView = expandableList.getChildAt(npos
- expandableList.getFirstVisiblePosition());
indicatorGroupHeight = groupView.getHeight();
}
if (indicatorGroupHeight == 0) {
return;
}
// if (isExpanded) {
if (count_expand > 0) {
the_group_expand_position = groupPos;
group_content.setText(groupData.get(the_group_expand_position)
.get("group_text"));
if (the_group_expand_position != groupPos||!expandableList.isGroupExpanded(groupPos)) {
view_flotage.setVisibility(View.GONE);
} else {
view_flotage.setVisibility(View.VISIBLE);
}
}
if (count_expand == 0) {
view_flotage.setVisibility(View.GONE);
}
}
if (the_group_expand_position == -1) {
return;
}
/**
* calculate point (0,indicatorGroupHeight)
*/
int showHeight = getHeight();
MarginLayoutParams layoutParams = (MarginLayoutParams) view_flotage
.getLayoutParams();
// 得到悬浮的条滑出屏幕多少
layoutParams.topMargin = -(indicatorGroupHeight - showHeight);
view_flotage.setLayoutParams(layoutParams);
}
有的手机品牌不一样可能会出现如下情况:
所以需要加上如下代码:
//防止三星,魅族等手机第一个条目可以一直往下拉,父条目和悬浮同时出现的问题
if(firstVisibleItem==0){
view_flotage.setVisibility(View.GONE);
}
--------------------------------------------------------------------------------
最近有些网友需要代码,现在附上下载地址.http://download.csdn.net/detail/johnny901114/4513242
欢迎转载:http://blog.csdn.net/johnny901114/article/details/7841685