Java tutorial
/* * Copyright (C) 2014 The Android Open Source Project * * 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.johnnyyin.viewdraghelperdemo; import android.content.Context; import android.support.v4.view.ViewCompat; import android.support.v4.widget.ViewDragHelper; import android.util.AttributeSet; import android.util.TypedValue; import android.view.MotionEvent; import android.view.View; import android.widget.FrameLayout; import java.util.ArrayList; import java.util.List; /** * A {@link FrameLayout} that allows the user to drag and reposition child views. */ public class DragFrameLayout extends FrameLayout { /** * The list of {@link View}s that will be draggable. */ private List<View> mDragViews; /** * The {@link DragFrameLayoutController} that will be notify on drag. */ private DragFrameLayoutController mDragFrameLayoutController; private ViewDragHelper mDragHelper; public DragFrameLayout(Context context) { this(context, null, 0); } public DragFrameLayout(Context context, AttributeSet attrs) { this(context, attrs, 0); } public DragFrameLayout(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); mDragViews = new ArrayList<View>(); /** * Create the {@link ViewDragHelper} and set its callback. */ mDragHelper = ViewDragHelper.create(this, 0.5f, new ViewDragHelper.Callback() { @Override public boolean tryCaptureView(View child, int pointerId) { return mDragViews.contains(child); } @Override public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) { super.onViewPositionChanged(changedView, left, top, dx, dy); } @Override public int clampViewPositionHorizontal(View child, int left, int dx) { return child.getLeft(); } @Override public int clampViewPositionVertical(View child, int top, int dy) { return top < 0 ? 0 : top; } @Override public void onViewCaptured(View capturedChild, int activePointerId) { super.onViewCaptured(capturedChild, activePointerId); if (mDragFrameLayoutController != null) { mDragFrameLayoutController.onDragDrop(true); } } @Override public void onViewReleased(View releasedChild, float xvel, float yvel) { int finalTop = 0; if (releasedChild.getTop() >= releasedChild.getHeight() / 2) { finalTop = releasedChild.getHeight(); } mDragHelper.settleCapturedViewAt(releasedChild.getLeft(), finalTop); invalidate(); if (mDragFrameLayoutController != null) { mDragFrameLayoutController.onDragDrop(false); } } }); mDragHelper.setMinVelocity( TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 400, getResources().getDisplayMetrics())); } @Override public boolean onInterceptTouchEvent(MotionEvent ev) { final int action = ev.getActionMasked(); if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) { mDragHelper.cancel(); return false; } return mDragHelper.shouldInterceptTouchEvent(ev); } @Override public boolean onTouchEvent(MotionEvent ev) { mDragHelper.processTouchEvent(ev); return true; } @Override public void computeScroll() { if (mDragHelper.continueSettling(true)) { ViewCompat.postInvalidateOnAnimation(this); } } /** * Adds a new {@link View} to the list of views that are draggable within the container. * * @param dragView the {@link View} to make draggable */ public void addDragView(View dragView) { mDragViews.add(dragView); } /** * Sets the {@link DragFrameLayoutController} that will receive the drag events. * * @param dragFrameLayoutController a {@link DragFrameLayoutController} */ public void setDragFrameController(DragFrameLayoutController dragFrameLayoutController) { mDragFrameLayoutController = dragFrameLayoutController; } /** * A controller that will receive the drag events. */ public interface DragFrameLayoutController { public void onDragDrop(boolean captured); } }