Java tutorial
/* * Copyright (c) 2016 <kaku201313@163.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.mrym.newsbulletion.widget; import android.content.Context; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v4.view.ViewCompat; import android.support.v4.widget.ViewDragHelper; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; import android.view.ViewConfiguration; import android.widget.FrameLayout; public class PullBackLayout extends FrameLayout { private final ViewDragHelper dragger; // http://blog.csdn.net/lmj623565791/article/details/46858663 private final int minimumFlingVelocity; @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, 1f / 8f, new ViewDragCallback()); // 1f / 8f???? minimumFlingVelocity = ViewConfiguration.get(context).getScaledMinimumFlingVelocity(); } public void setCallback(@Nullable Callback callback) { this.callback = callback; } @Override public boolean onInterceptTouchEvent(MotionEvent ev) { try { return dragger.shouldInterceptTouchEvent(ev); } catch (Exception e) { e.printStackTrace(); } return false; } @Override public boolean onTouchEvent(@NonNull MotionEvent event) { try { dragger.processTouchEvent(event); return true; } catch (Exception e) { e.printStackTrace(); } return false; } @Override public void computeScroll() { if (dragger.continueSettling(true)) { ViewCompat.postInvalidateOnAnimation(this); } } public interface Callback { void onPullStart(); void onPull(float progress); void onPullCancel(); void onPullComplete(); } private class ViewDragCallback extends ViewDragHelper.Callback { @Override public boolean tryCaptureView(View child, int pointerId) { return true; } @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 onViewCaptured(View capturedChild, int activePointerId) { if (callback != null) { callback.onPullStart(); } } @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) { int slop = yvel > minimumFlingVelocity ? getHeight() / 6 : getHeight() / 3; if (releasedChild.getTop() > slop) { if (callback != null) { callback.onPullComplete(); } } else { if (callback != null) { callback.onPullCancel(); } dragger.settleCapturedViewAt(0, 0); invalidate(); } } } }