一:效果
滑动前 滑动一段距离之后二:实现方式
原理很简单,就是把这两个tab盖在ListView上面,一开始是不可见的,随着listview的滑动,当listview的tab和这两个tab重合时,显示这两个tab,隐藏listview上的tab,因此这种实现方式的核心是获取listview的滑动距离
private int mCurrentfirstVisibleItem;
private SparseArray recordSp = new SparseArray(0);//设置容器大小,默认是10
class MyOnScrollListener implements AbsListView.OnScrollListener {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
mCurrentfirstVisibleItem = firstVisibleItem;
View firstView = view.getChildAt(0);//获取当前最顶部的item
if (firstView != null) {
ItemRecod itemRecord = (ItemRecod) recordSp.get(firstVisibleItem);
if (itemRecord == null) {
itemRecord = new ItemRecod();
}
//关于这个height和top到底是多少,怎么变化,我们可以通过日志来看,一目了然
itemRecord.height = firstView.getHeight();//获取当前最顶部Item的高度
itemRecord.top = firstView.getTop();//获取对应item距离顶部的距离
recordSp.append(firstVisibleItem, itemRecord);//添加键值对设置值
int ScrollY = getScrollY();//这就是滑动的距离,我们可以根据这个距离做很多事
}
}
}
private int getScrollY() {
int height = 0;
for (int i = 0; i < mCurrentfirstVisibleItem; i++) {
ItemRecod itemRecod = (ItemRecod) recordSp.get(i);
if (itemRecod != null) {
height += itemRecod.height;
}
}
ItemRecod itemRecod = (ItemRecod) recordSp.get(mCurrentfirstVisibleItem);
if (null == itemRecod) {
itemRecod = new ItemRecod();
}
return height - itemRecod.top;
}
private class ItemRecod {
int height = 0;
int top = 0;
}
备注:
效果图取自猫爪app
Android下载地址:http://www.maozhuar.com/download
apple可在App Store下载
网友评论