Java tutorial
/* * Copyright 2015 XiNGRZ <chenxingyu92@gmail.com> * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.fuxuemingzhu.threaduitest.widget; import android.content.Context; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v4.widget.ViewDragHelper; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; import android.widget.FrameLayout; public class PullBackLayout extends FrameLayout { private final ViewDragHelper dragger; @Nullable private Callback callback; public PullBackLayout(Context context) { this(context, null); } public PullBackLayout(Context context, AttributeSet attrs) { this(context, attrs, 0); } public PullBackLayout(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); dragger = ViewDragHelper.create(this, new ViewDragCallback()); } public void setCallback(@Nullable Callback callback) { this.callback = callback; } @Override public boolean onInterceptTouchEvent(MotionEvent ev) { return dragger.shouldInterceptTouchEvent(ev); } @Override public boolean onTouchEvent(@NonNull MotionEvent event) { dragger.processTouchEvent(event); return true; } @Override public void computeScroll() { if (dragger.continueSettling(true)) { postInvalidateOnAnimation(); } } public interface Callback { void onPull(float progress); void onPullDown(); boolean canPullDown(); } private class ViewDragCallback extends ViewDragHelper.Callback { @Override public boolean tryCaptureView(View child, int pointerId) { return callback != null && callback.canPullDown(); } @Override public int clampViewPositionHorizontal(View child, int left, int dx) { return 0; } @Override public int clampViewPositionVertical(View child, int top, int dy) { return Math.max(0, top); } @Override public int getViewHorizontalDragRange(View child) { return 0; } @Override public int getViewVerticalDragRange(View child) { return getHeight(); } @Override public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) { if (callback != null) { callback.onPull((float) top / (float) getHeight()); } } @Override public void onViewReleased(View releasedChild, float xvel, float yvel) { if (callback != null && (yvel > 0 || (yvel == 0 && releasedChild.getTop() > (float) getHeight() / 3f))) { callback.onPullDown(); } else { dragger.settleCapturedViewAt(0, 0); invalidate(); } } } }