Android初级教程_用ExpandableListView实现相似QQ好友列表

咱们知道QQ好友列表分类使用ExpandableListView实现的,可是它在此基础上作改进,如打开分组,滑动时组标题会悬浮在最上端.java

方便用户关闭分组.效果相似于下图:ide





实现代码以下所示:spa

@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);
	}

有的手机品牌不同可能会出现以下状况:



因此须要加上以下代码:.net

//防止三星,魅族等手机第一个条目能够一直往下拉,父条目和悬浮同时出现的问题
		if(firstVisibleItem==0){
			view_flotage.setVisibility(View.GONE);
		}


--------------------------------------------------------------------------------code


最近有些网友须要代码,如今附上下载地址.http://download.csdn.net/detail/johnny901114/4513242
blog



欢迎转载:http://blog.csdn.net/johnny901114/article/details/7841685get