PinnedSectionListView:(固定部分的ListView)分组的listView滑动中固定组标题的实现
来源:互联网 发布:小米手环数据同步失败 编辑:程序博客网 时间:2024/05/12 16:58
在很多应用中,看到这样的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
- PinnedSectionListView:(固定部分的ListView)分组的listView滑动中固定组标题的实现
- PinnedSectionListView:分组的listView滑动中固定组标题的实现
- PinnedSectionListView:分组的listView滑动中固定组标题的实现
- PinnedSectionListView(分组+可固定标题的ListView)
- 分组的listView滑动中固定组标题
- 实现分组+固定表头的ListView之PinnedHeaderListView
- 固定ListView的高度
- Android Listview分组特效:滑动分组标题当前固定,并随内容滑动(andrroid原生电话本实现方法)
- ListView中固定item的高度
- ListView的固定选中效果
- 带固定列支持横向滑动的ListView
- 滑动效果——ListView 滑动中标题固定&标题缩放效果
- 基于事件分发机制实现的表头固定的Listview
- ListView的分组实现
- ListView的分组实现
- ListView的分组实现
- 滑动更改ListView的标题
- WPF中怎样固定ListView中列的宽度?
- CPPUnit下载编译
- 下一代ERP技术
- 【CodeForce #239 Div2】408A——Line to Cashier
- 白领必看的十种职场致命毒药
- 表格的基本特性
- PinnedSectionListView:(固定部分的ListView)分组的listView滑动中固定组标题的实现
- svn的merge使用例子
- 【CodeForce #239 Div2】408B——Garland
- PHP Global变量定义当前页面的全局变量
- 武大网络预赛 Problem 1545 - I - Twenty-four
- cf——B. Garland
- hadoop集群搭建
- AFJSONRequestOperation 访问总是到fail回调的处理
- unity3D游戏开发实战原创视频讲座系列5之狼烟狙击游戏开发