编写联系人list
来源:互联网 发布:大连知你创业基地 编辑:程序博客网 时间:2024/06/09 17:27
控件https://github.com/beworker/pinned-section-listview
在很多应用中,看到这样的listview:listview滑动过程中分组标题固定在上方,当第二个组滑上来时,第一个组才跟着上滑,下一个组固定,直到该组也滑出上边缘。世上无难事只怕有心人,在github上就有人做出来了,而且效果很好(后来发现安卓自带应用中联系人应用就是这样的,估计github的作者也是仿照着联系人做出来的吧)。
先看截图:
PinnedSectionListView继承自listview,众所周知listview的每个子view都是按顺序跟着滚动的,要实现联系人listview的效果还真的找不到思路。看了PinnedSectionListView之后,感觉要改造一个现有的控件,一般都是通过重绘子view来实现的。ViewGroup(ListView继承自它)重绘子view的方法是dispatchDraw。
看看PinnedSectionListView在dispatchDraw中有那些特别的处理:
@Override
protected void dispatchDraw(Canvas canvas) {
super
.dispatchDraw(canvas);
if
(mPinnedSection !=
null
) {
// prepare variables
int pLeft = getListPaddingLeft();
int pTop = getListPaddingTop();
View view = mPinnedSection.view;
// draw child
canvas.save();
int clipHeight = view.getHeight() +
(mShadowDrawable ==
null
? 0 : Math.min(mShadowHeight, mSectionsDistanceY));
canvas.clipRect(pLeft, pTop, pLeft + view.getWidth(), pTop + clipHeight);
canvas.translate(pLeft, pTop + mTranslateY);
drawChild(canvas, mPinnedSection.view, getDrawingTime());
if
(mShadowDrawable !=
null
&& mSectionsDistanceY > 0) {
mShadowDrawable.setBounds(mPinnedSection.view.getLeft(),
mPinnedSection.view.getBottom(),
mPinnedSection.view.getRight(),
mPinnedSection.view.getBottom() + mShadowHeight);
mShadowDrawable.draw(canvas);
}
canvas.restore();
}
}
关键在于canvas.translate(pLeft, pTop + mTranslateY);
意思是在绘制mPinnedSection
的时候,listview滑动了多长的距离,就将canvas移动多少的距离,使mPinnedSection
始终在可见的范围内固定不变。
使用方法:
1.在xml布局文件中将ListView替换成PinnedSectionListView
<com.hb.views.PinnedSectionListView
android:id=
"@android:id/list"
android:layout_width=
"match_parent"
android:layout_height=
"wrap_content"
/>
2.让你的ListAdapter继承PinnedSectionListAdapter
接口,最简单的做法是只增加isItemViewTypePinned
方法,该方法必须在item为pinned的情况下返回true。
// Our adapter class implements 'PinnedSectionListAdapter' interface
class MyPinnedSectionListAdapter extends BaseAdapter implements PinnedSectionListAdapter {
...
// We implement this method to return 'true' for all view types we want to pin
@Override
public boolean isItemViewTypePinned(int viewType) {
return
viewType == <type to be pinned>;
}
}
项目地址:https://github.com/beworker/pinned-section-listview
最后推荐一个demo:http://blog.csdn.net/anddroid_lanyan/article/details/41895631
- 编写联系人list
- C#编写的联系人管理软件,SQLite数据库
- 自己编写的List
- c编写List Components
- 联系人
- 联系人
- 联系人
- 联系人
- 使用VC6编写程序导出Outlook联系人信息
- 读取本机电话簿联系人信息返回一个list
- 编写自己的list容器
- 编写可编辑的List控件
- Android中用layer-list编写阴影效果
- Android中用layer-list编写阴影效果
- Android中用layer-list编写阴影效果
- Linear List 的编写与实现
- Android中用layer-list编写阴影效果
- Android中用layer-list编写阴影效果
- Lucene 查询索引库
- 反距离插值(Inverse Distance Weighted)
- 强大的ORM之Realm基础使用总结
- redis持久化详解
- hdu 1116 poj 1386 欧拉回路并查集
- 编写联系人list
- 组播MAC地址
- Spring 各个jar包的作用
- 关于H5唤醒APP的功能实现(千辛万苦啊!)
- List<Object>删除某一个Object
- 用pybrain构建BP神经网络
- Cannot find the declaration of element 'beans'. 的解决方法
- Mac 下 Intelij idea 的快捷键
- CentOS6.4+Nginx1.4.2+MySQL5.6.13+PHP5.5.3+xCache3.03(最新LNMP编译安装过程)