…
}
这是我们的状态接口IDecoratorState,其提供了3个方法,IdleState、OverScrollingState、BounceBackState都是它的具体实现类,符合状态模式的思想:
protected interface IDecoratorState {
// 处理move事件
boolean handleMoveTouchEvent(MotionEvent event);
// 处理up事件
boolean handleUpTouchEvent(MotionEvent event);
// 事件结束后的动画处理
void handleTransitionAnim(IDecoratorState fromState);
}
初始化我们定义的变量,没有什么特殊的操作,只是一些各自属性的赋值,具体见下文:
private void initParams() {
mBounceBackState = new BounceBackState();
mOverScrollingState = new OverScrollingState();
mCurrentState = mIdleState = new IdleState();
attach();
}
这是我们的attach,添加触摸监听,并去掉滚动到边缘的光晕效果:
@SuppressLint(“ClickableViewAccessibility”)
public void attach() {
mRecyclerView.setOnTouchListener(this);
mRecyclerView.setOverScrollMode(View.OVER_SCROLL_NEVER);
}
核心代码就是事件的监听了,需要我们处理onTouch事件,当手指按下滑动时,此时mCurrentState还处于初始状态,其会执行相应的handleMoveTouchEvent方法:
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE:
return mCurrentState.handleMoveTouchEvent(event);
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
return mCurrentState.handleUpOrCancelTouchEvent(event);
}
return false;
}
这是初始状态IdleState处理move的逻辑,主要做些校验工作,如果移动不满足要求,就将事件透出去,具体见下:
@Override
public boolean handleMoveTouchEvent(MotionEvent event) {
// 是否符合move要求,不符合不拦截事件
if (!initMotionAttribu