Android Open Source - android-training-tutorial Drag Layout






From Project

Back to project page android-training-tutorial.

License

The source code is released under:

MIT License

If you think the Android project android-training-tutorial listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.flavienlaurent.vdh;
/*from www .ja va2 s  .co  m*/
import android.content.Context;
import android.support.v4.view.MotionEventCompat;
import android.support.v4.widget.ViewDragHelper;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;

/**
 * Created by Flavien Laurent (flavienlaurent.com) on 23/08/13.
 */
public class DragLayout extends LinearLayout {

    private final ViewDragHelper mDragHelper;

    private View mDragView1;
    private View mDragView2;

    private boolean mDragEdge;
    private boolean mDragHorizontal;
    private boolean mDragCapture;
    private boolean mDragVertical;

    public DragLayout(Context context) {
        this(context, null);
    }

    public DragLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DragLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mDragHelper = ViewDragHelper.create(this, 1f, new DragHelperCallback());
    }

    @Override
    protected void onFinishInflate() {
        mDragView1 = findViewById(R.id.drag1);
        mDragView2 = findViewById(R.id.drag2);
    }

    public void setDragHorizontal(boolean dragHorizontal) {
        mDragHorizontal = dragHorizontal;
        mDragView2.setVisibility(View.GONE);
    }

    public void setDragVertical(boolean dragVertical) {
        mDragVertical = dragVertical;
        mDragView2.setVisibility(View.GONE);
    }

    public void setDragEdge(boolean dragEdge) {
        mDragHelper.setEdgeTrackingEnabled(ViewDragHelper.EDGE_LEFT);
        mDragEdge = dragEdge;
        mDragView2.setVisibility(View.GONE);
    }

    public void setDragCapture(boolean dragCapture) {
        mDragCapture = dragCapture;
    }

    private class DragHelperCallback extends ViewDragHelper.Callback {

        @Override
        public boolean tryCaptureView(View child, int pointerId) {
            if (mDragCapture) {
                return child == mDragView1;
            }
            return true;
        }

        @Override
        public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) {
            invalidate();
        }

        @Override
        public void onViewCaptured(View capturedChild, int activePointerId) {
            super.onViewCaptured(capturedChild, activePointerId);
        }

        @Override
        public void onViewReleased(View releasedChild, float xvel, float yvel) {
            super.onViewReleased(releasedChild, xvel, yvel);
        }

        @Override
        public void onEdgeTouched(int edgeFlags, int pointerId) {
            super.onEdgeTouched(edgeFlags, pointerId);
        }

        @Override
        public void onEdgeDragStarted(int edgeFlags, int pointerId) {
            if (mDragEdge) {
                mDragHelper.captureChildView(mDragView1, pointerId);
            }
        }

        @Override
        public int clampViewPositionVertical(View child, int top, int dy) {
            if (mDragVertical) {
                final int topBound = getPaddingTop();
                final int bottomBound = getHeight() - mDragView1.getHeight();

                final int newTop = Math.min(Math.max(top, topBound), bottomBound);

                return newTop;
            }
            return super.clampViewPositionVertical(child, top, dy);
        }

        @Override
        public int clampViewPositionHorizontal(View child, int left, int dx) {
            if (mDragHorizontal || mDragCapture || mDragEdge) {
                final int leftBound = getPaddingLeft();
                final int rightBound = getWidth() - mDragView1.getWidth();

                final int newLeft = Math.min(Math.max(left, leftBound), rightBound);

                return newLeft;
            }
            return super.clampViewPositionHorizontal(child, left, dx);
        }

    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        final int action = MotionEventCompat.getActionMasked(ev);
        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;
    }

}




Java Source Code List

com.flavienlaurent.vdh.DragActivity.java
com.flavienlaurent.vdh.DragLayout.java
com.flavienlaurent.vdh.HorizontalDragLayout.java
com.flavienlaurent.vdh.MainActivity.java
com.flavienlaurent.vdh.YoutubeActivity.java
com.flavienlaurent.vdh.YoutubeLayout.java
com.flavienlaurent.vdh.listener.SwipeDismissTouchListener.java
com.hb.views.PinnedSectionListView.java
fi.harism.curl.CurlActivity.java
fi.harism.curl.CurlMesh.java
fi.harism.curl.CurlPage.java
fi.harism.curl.CurlRenderer.java
fi.harism.curl.CurlView.java
im.ene.dev.zbroadcastreceiver2.MainActivity.java
im.ene.dev.zbroadcastreceiver2.receivers.CustomBroadcastReceiver.java
im.ene.dev.zbroadcastreceiver2.services.CustomBroadcastService.java
im.ene.dev.zbroadcastreceiver.MainActivity.java
im.ene.dev.zbroadcastreceiver.SecondActivity.java
im.ene.dev.zbroadcastreceiver.receivers.CustomBroadcastReceiver.java
im.ene.dev.zbroadcastreceiver.services.CustomBroadcastService.java
im.ene.dev.zserviceibinder.MainActivity.java
im.ene.dev.zserviceibinder.services.CustomService.java
im.ene.pinnedheaderlistviewsample.MainActivity.java
im.ene.samplevideoplayer.MainActivity.java
im.ene.samplevideoplayer.constant.Constant.java
im.ene.samplevideoplayer.views.CustomSurfaceView.java
im.ene.samplevideoplayer.views.VideoControllerView.java
im.ene.zaidlservicesample.MainActivity.java
im.ene.zaidlservicesample.Second.java
im.ene.zanimationsample.MainActivity.java
im.ene.zcustomvideoview.MainActivity.java
im.ene.zcustomvideoview.views.CustomVideoView.java
im.ene.zcustomvideoview.views.VideoControllerView.java
im.ene.zintenttutorial.MainActivity.java
im.ene.zintenttutorial.broadcastreceiver.CustomBroadcastReceiver.java
im.ene.zintenttutorial.services.CustomService.java
im.ene.zviewpager.MainActivity.java