滑动底部工具条显示与隐藏

public class ScrollerBottomLayoutDelegation {

private NestedScrollViewmNestedScroller;
private ViewGroup mBottomLayout;

private ScrollerBottomLayoutDelegation(NestedScrollView scroller, ViewGroup view){
this.mNestedScroller = scroller;
this.mBottomLayout = view;
init();
}

/**
* 开始初始化代理
*/
private void init() {
mNestedScroller.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
@Override
public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
int dy = scrollY - oldScrollY;
// 第一次调用的时候 dy = nowYOffset
// 随后调用的时候可以做是变化量dy的叠加
// 在移动的过程中mBottomLayout.getTop()始终不会变
int nowYOffset = (int) (dy + mBottomLayout.getTranslationY());
Log.d("@@@", "dy = " + dy + " , nowYOffset = " + nowYOffset + " , top = " + mBottomLayout.getTop());
// scrollTo()scrollBy():移动view的显示位置,并不移动它的点击位置
// setTranslationX():设置x轴平衡距离,view的点击位置和显示位置同时移动
// 取消当前正在运行或者挂起的属性动画
ViewCompat.animate(mBottomLayout).cancel();
if (dy > 0) { // 手指向顶部移动 布局隐藏
if (nowYOffset < mBottomLayout.getHeight()) {
mBottomLayout.setTranslationY(nowYOffset);
} else {
mBottomLayout.setTranslationY(mBottomLayout.getHeight());
}
} else if (dy < 0) { // 手指向底部移动
if (nowYOffset < 0) {
mBottomLayout.setTranslationY(0);
} else {
mBottomLayout.setTranslationY(nowYOffset);
}
}
}
});

mNestedScroller.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
// positive to check scrolling down 经测试:如果已经滑动到底部时再把手指往上滑回调该方法
if (!mNestedScroller.canScrollVertically(1)) {
animateHide();
return false;
}
// Negative to check scrolling up 经测试:如果已经滑动到顶部时再把手指往下滑回调该方法
if (!mNestedScroller.canScrollVertically(-1)) {
animateShow();
return false;
}
if (mNestedScroller.getScrollY() <= 0) return false;
if (mBottomLayout.getTranslationY() >= mBottomLayout.getHeight() / 2) {
// 手指向顶部滑动
animateHide();
} else {
animateShow();
}
}
return false;
}
});
}

public static void delegation(NestedScrollView scroller, ViewGroup view){
new ScrollerBottomLayoutDelegation(scroller, view);
}

// y = top + translationY;
private void animateShow(){
mBottomLayout.animate()
.translationY(0)
.setInterpolator(new LinearInterpolator())
.setDuration(180);
}

private void animateHide(){
mBottomLayout.animate()
.translationY(mBottomLayout.getHeight())
.setInterpolator(new LinearInterpolator())
.setDuration(180);
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值