动手分析安卓仿QQ联系人列表TreeView控件
因项目需要需要用到仿QQ联系人列表的控件样式,于是网上找到一个轮子(https://github.com/TealerProg/TreeView),工作完成现在简单分析一下这个源码。
一、 需要用到的知识如下:
package com.markmao.treeview.widget; import android.view.View; /**
* Update interface TreeView's header .
*
* @author markmjw
* @date 2014-01-04
*/
public interface ITreeViewHeaderUpdater {
/** Header Gone. */
public static final int STATE_GONE = 0x00;
/** Header Visible. */
public static final int STATE_VISIBLE_ALL = 0x01;
/** Header Push up. */
public static final int STATE_VISIBLE_PART = 0x02; /**
* Get TreeView's header state.
*
* @param groupPosition The group position.
* @param childPosition The child position.
* @return {@link #STATE_GONE}, {@link #STATE_VISIBLE_ALL},
* {@link #STATE_VISIBLE_PART}
*/
public int getHeaderState(int groupPosition, int childPosition); /**
* Update TreeView's header.
*
* @param header The TreeView's header view.
* @param groupPosition The group position.
* @param childPosition The child position.
* @param alpha The header's alpha value.
*/
public void updateHeader(View header, int groupPosition, int childPosition, int alpha); /**
* The header view onClick.
*
* @param groupPosition The group position.
* @param status {@link #STATE_GONE}, {@link #STATE_VISIBLE_ALL},
* {@link #STATE_VISIBLE_PART}
*/
public void onHeaderClick(int groupPosition, int status); /**
* Get the header's state on click.
*
* @param groupPosition The group position.
* @return {@link #STATE_GONE}, {@link #STATE_VISIBLE_ALL},
* {@link #STATE_VISIBLE_PART}
*/
public int getHeaderClickStatus(int groupPosition);
}
主要定义了三个状态:STATE_GONE:HeaderView处于隐藏不显示状态,STATE_VISIBLE:HeaderView处于显示状态,STATE_VISIBLE_PART:HeaderView处于显示且需要向上推起的临界状态。
2、BaseTreeViewAdapter实现ITreeViewHeaderUpdater接口
package com.markmao.treeview.widget; import android.util.Log;
import android.util.SparseIntArray;
import android.widget.BaseExpandableListAdapter; /**
* The base adapter for TreeView.
*
* @author markmjw
* @date 2014-01-04
*/
public abstract class BaseTreeViewAdapter extends BaseExpandableListAdapter implements
ITreeViewHeaderUpdater {
protected TreeView mTreeView; protected SparseIntArray mGroupStatusArray; public BaseTreeViewAdapter(TreeView treeView) {
mTreeView = treeView;
mGroupStatusArray = new SparseIntArray();
} @Override
public int getHeaderState(int groupPosition, int childPosition) {
final int childCount = getChildrenCount(groupPosition); if (childPosition == childCount - 1) {
return STATE_VISIBLE_PART;
} else if (childPosition == -1 && !mTreeView.isGroupExpanded(groupPosition)) {
return STATE_GONE;
} else {
return STATE_VISIBLE_ALL;
}
} @Override
public void onHeaderClick(int groupPosition, int status) {
mGroupStatusArray.put(groupPosition, status);
} @Override
public int getHeaderClickStatus(int groupPosition) {
return mGroupStatusArray.get(groupPosition, STATE_GONE);
}
}
主要方法:getHeaderState方法,当childPosition==childCount-1条件的时候(及 HeaderView处于显示且需要向上推起的临界状态)时返回STATE_VISIBLE_PART
3、TreeView
package com.markmao.treeview.widget; import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnGroupClickListener; /**
* This widget extends {@link android.widget.ExpandableListView}, just like TreeView(IOS).
*
* @see android.widget.ExpandableListView
* @author markmjw
* @date 2014-01-03
*/
public class TreeView extends ExpandableListView implements OnScrollListener, OnGroupClickListener {
private static final int MAX_ALPHA = 255; private ITreeViewHeaderUpdater mUpdater; private View mHeaderView; private boolean mHeaderVisible; private int mHeaderWidth; private int mHeaderHeight; public TreeView(Context context) {
super(context);
init();
} public TreeView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
} public TreeView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
} private void init() {
setSmoothScrollbarEnabled(true); setOnScrollListener(this);
setOnGroupClickListener(this);
} /**
* Sets the list header view
*
* @param view
*/
public void setHeaderView(View view) {
mHeaderView = view;
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams
.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
view.setLayoutParams(lp); if (mHeaderView != null) {
setFadingEdgeLength(0);
} requestLayout();
} @Override
public void setAdapter(ExpandableListAdapter adapter) {
super.setAdapter(adapter); if(adapter instanceof ITreeViewHeaderUpdater) {
mUpdater = (ITreeViewHeaderUpdater) adapter;
} else {
throw new IllegalArgumentException("The adapter must instanceof ITreeViewHeaderUpdater.");
}
} @Override
public boolean onTouchEvent(MotionEvent ev) {
// header view is visible
if (mHeaderVisible) {
float downX = 0;
float downY = 0; switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
downX = ev.getX();
downY = ev.getY();
if (downX <= mHeaderWidth && downY <= mHeaderHeight) {
return true;
}
break; case MotionEvent.ACTION_UP:
float x = ev.getX();
float y = ev.getY();
float offsetX = Math.abs(x - downX);
float offsetY = Math.abs(y - downY);
// the touch event under header view
if (x <= mHeaderWidth && y <= mHeaderHeight && offsetX <= mHeaderWidth &&
offsetY <= mHeaderHeight) {
if (mHeaderView != null) {
onHeaderViewClick();
} return true;
}
break; default:
break;
}
} return super.onTouchEvent(ev);
} @Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
int status = mUpdater.getHeaderClickStatus(groupPosition); switch (status) {
case ITreeViewHeaderUpdater.STATE_GONE:
mUpdater.onHeaderClick(groupPosition, ITreeViewHeaderUpdater.STATE_VISIBLE_ALL);
break; case ITreeViewHeaderUpdater.STATE_VISIBLE_ALL:
mUpdater.onHeaderClick(groupPosition, ITreeViewHeaderUpdater.STATE_GONE);
break; case ITreeViewHeaderUpdater.STATE_VISIBLE_PART:
// ignore
break; default:
break;
} return false;
} @Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec); if (mHeaderView != null) {
measureChild(mHeaderView, widthMeasureSpec, heightMeasureSpec);
mHeaderWidth = mHeaderView.getMeasuredWidth();
mHeaderHeight = mHeaderView.getMeasuredHeight();
}
} private int mOldState = -1; @Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom); final long listPosition = getExpandableListPosition(getFirstVisiblePosition());
final int groupPos = ExpandableListView.getPackedPositionGroup(listPosition);
final int childPos = ExpandableListView.getPackedPositionChild(listPosition);
Log.v("TreeView--onlayout分析:",String.format("返回所选择的List %d,返回所选择的组项 %d,返回所选择的子项 %d",(int)listPosition,groupPos,childPos));
int state = mUpdater.getHeaderState(groupPos, childPos);
if (mHeaderView != null && mUpdater != null && state != mOldState) {
mOldState = state;
mHeaderView.layout(0, 0, mHeaderWidth, mHeaderHeight);
} updateHeaderView(groupPos, childPos); } @Override
protected void dispatchDraw(Canvas canvas) {
super.dispatchDraw(canvas);
if (mHeaderVisible) {
// draw header view
drawChild(canvas, mHeaderView, getDrawingTime());
Log.v("TreeView--dispatchDraw分析:","重新绘制mHeaderView");
}
} @Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
int totalItemCount) {
final long listPosition = getExpandableListPosition(firstVisibleItem);
int groupPos = ExpandableListView.getPackedPositionGroup(listPosition);
int childPos = ExpandableListView.getPackedPositionChild(listPosition); updateHeaderView(groupPos, childPos);
} @Override
public void onScrollStateChanged(AbsListView view, int scrollState) { } private void updateHeaderView(int groupPosition, int childPosition) {
if (mHeaderView == null || mUpdater == null || ((ExpandableListAdapter) mUpdater)
.getGroupCount() == 0) {
return;
} int state = mUpdater.getHeaderState(groupPosition, childPosition); switch (state) {
case ITreeViewHeaderUpdater.STATE_GONE: {
mHeaderVisible = false;
break;
} case ITreeViewHeaderUpdater.STATE_VISIBLE_ALL: {
mUpdater.updateHeader(mHeaderView, groupPosition, childPosition, MAX_ALPHA); if (mHeaderView.getTop() != 0) {
mHeaderView.layout(0, 0, mHeaderWidth, mHeaderHeight);
} mHeaderVisible = true;
break;
} case ITreeViewHeaderUpdater.STATE_VISIBLE_PART: {
// a part of header view visible
View firstView = getChildAt(0);
int bottom = null != firstView ? firstView.getBottom() : 0; int headerHeight = mHeaderView.getHeight();
int topY;
int alpha; if (bottom < headerHeight) {
topY = (bottom - headerHeight);
alpha = MAX_ALPHA * (headerHeight + topY) / headerHeight;
} else {
topY = 0;
alpha = MAX_ALPHA;
} mUpdater.updateHeader(mHeaderView, groupPosition, childPosition, alpha);
Log.v("TreeView--updateHeaderView分析:",String.format("bottom=%d,headerHeight=%d,topY=%d,getTop=%d,Header Push up",bottom,headerHeight,topY,
mHeaderView.getTop()));
if (mHeaderView.getTop() != topY) {
mHeaderView.layout(0, topY, mHeaderWidth, mHeaderHeight + topY);
}
mHeaderVisible = true;
break;
}
}
} private void onHeaderViewClick() {
long packedPosition = getExpandableListPosition(getFirstVisiblePosition()); int groupPosition = ExpandableListView.getPackedPositionGroup(packedPosition); int status = mUpdater.getHeaderClickStatus(groupPosition);
if (ITreeViewHeaderUpdater.STATE_VISIBLE_ALL == status) {
collapseGroup(groupPosition);
mUpdater.onHeaderClick(groupPosition, ITreeViewHeaderUpdater.STATE_GONE);
} else {
expandGroup(groupPosition);
mUpdater.onHeaderClick(groupPosition, ITreeViewHeaderUpdater.STATE_VISIBLE_ALL);
} setSelectedGroup(groupPosition);
}
}
if (mHeaderView.getTop() != topY) {
mHeaderView.layout(0, topY, mHeaderWidth, mHeaderHeight + topY);
}
这段代码起到了到达临界状态时候,起到推送mHeaderView上滑动的作用
⑤、onTouchEvent方法,TreeView中实现该方法,根据事件分发机制适时的捕获用户点击事件,调用onHeaderViewClick方法,起到点击GroupItem以展开和收起对应组的效果。
动手分析安卓仿QQ联系人列表TreeView控件的更多相关文章
- WPF 自定义TreeView控件样式,仿QQ联系人列表
一.前言 TreeView控件在项目中使用比较频繁,普通的TreeView并不能满足我们的需求.因此我们需要滴对TreeView进行改造.下面的内容将介绍仿QQ联系人TreeView样式及TreeVi ...
- Android自定义View,高仿QQ音乐歌词滚动控件!
最近在以QQ音乐为样板做一个手机音乐播放器,源码下篇博文放出.今天我想聊的是这个QQ音乐播放器中歌词显示控件的问题,和小伙伴们一起来探讨怎么实现这个歌词滚动的效果.OK,废话不多说,先来看看效果图: ...
- VB TreeView控件使用详解
来源:http://www.newxing.com/Tech/Program/VisualBasic/TreeView_587.html 三小时快速掌握TreeView树状控件的使用.能不能掌握控件的 ...
- 如何:使用TreeView控件实现树结构显示及快速查询
本文主要讲述如何通过使用TreeView控件来实现树结构的显示,以及树节点的快速查找功能.并针对通用树结构的数据结构存储进行一定的分析和设计.通过文本能够了解如何存储层次结构的数据库设计,如何快速使用 ...
- Visual Studio 2010下ASPX页面的TreeView控件循环遍历
如果维护一个老系统就总会遇到各种问题,而这次是TreeView的循环遍历.对于Visual Studio2010上aspx页面的TreeView控件,我感受到了什么叫集微软之大智慧.与二叉树型不一样. ...
- Windows Phone 8.1 新特性 - 控件之列表选择控件
本篇我们来介绍Windows Phone 8.1 新特性中的列表选择控件. 在Windows Phone 8 时代,大家都会使用 LongListSelector 来实现列表选择控件,对数据进行分组显 ...
- C#Winform中treeView控件使用总结
1.如何展开结点时改变图标(注意:不是选中时) 要在目录中使用图标首先要加入一个控件ImageList(命名为imageList1),然后可以按图片的index或名称引用图片. 然后需要在TreeVi ...
- WPF 将数据源绑定到TreeView控件出现界面卡死的情况
首先来谈一下实现将自定义的类TreeMode绑定到TreeView控件上的一个基本的思路,由于每一个节点都要包含很多自定义的一些属性信息,因此我们需要将该类TreeMode进行封装,TreeView的 ...
- ComboBox中如何嵌套TreeView控件
在ComboBox中嵌套TreeView控件,有时候我们在设计界面的时候,由于界面设计的需要,我们需要将TreeView控件嵌套在ComboBox中,因为TreeView控件实在是太占用地方了,要 ...
随机推荐
- Android开发高手课笔记 - 01 崩溃优化(上):关于“崩溃”那点事
Android 的两种崩溃 Java 崩溃就是在 Java 代码中,出现了未捕获的异常,导致程序异常退出 Native 崩溃一般都是因为在 Native 代码中访问非法地址,也可能是地址对齐出了问题, ...
- IIS中实现http自动转换到https
IIS中实现http自动转换到https修改以下文件:C:\WINDOWS\Help\iisHelp\common\403-4.htm 为以下内容<!DOCTYPE HTML PUBLIC &q ...
- Android ExpandableListView的使用详解
ExpandableListView(可扩展的ListView) ExpandableListVivew是ListView的子类,它在普通ListView的基础上进行了扩展,它把应用中的列表项分为几组 ...
- MVC5+EasyUI+EF6+Linq通用权限系统出炉(1)
1.先晒一下结构吧,
- CDR X8图框精确剪裁在哪?
对于CorelDRAW,刚从低版本升级为高版本的同学可能一下子理不清方向,因为在CorelDRAW X8中有很多功能命令做了整改和位置的变化.很多用户反映,CDR中的图框精确剪裁不见了,然而并不是该命 ...
- Python 之动态添加属性以及方法
import types class Person(object): def __init__(self, newName, newAge): self.name = newName self.age ...
- Java中的接口详解
接口 是Java语言中一种引用类型,是方法的集合,如果说类的内部封装了成员变量.构造方法和成员方法,那么接口的内部主要就是封装了方法,包含抽象方法(JDK 7及以前),默认方法和静态方法(JDK 8) ...
- STL中队列queue的用法
头文件:#include <queue> 建立一个队列queue < 类型 > q 加入一个新的元素q.push(a) 询问队首元素q.front() 弹出队首元素q.pop( ...
- python爬虫17 | 听说你又被封 ip 了,你要学会伪装好自己,这次说说伪装你的头部
这两天 有小伙伴问小帅b 为什么我爬取 xx 网站的时候 不返回给我数据 而且还甩一句话给我 “系统检测到您频繁访问,请稍后再来” 小帅b看了一下他的代码 ): requests.get(url) 瞬 ...
- Maven pom 配置简介
1. groupId artifactId version 2. dependencies 3. plugins http://shmilyaw-hotmail-com.iteye.com/blog/ ...