Example usage for android.view MotionEvent ACTION_DOWN

List of usage examples for android.view MotionEvent ACTION_DOWN

Introduction

In this page you can find the example usage for android.view MotionEvent ACTION_DOWN.

Prototype

int ACTION_DOWN

To view the source code for android.view MotionEvent ACTION_DOWN.

Click Source Link

Document

Constant for #getActionMasked : A pressed gesture has started, the motion contains the initial starting location.

Usage

From source file:com.andview.refreshview.swipe.SwipeMenuLayout.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    if (mVelocityTracker == null)
        mVelocityTracker = VelocityTracker.obtain();
    mVelocityTracker.addMovement(ev);/*from  w w  w. j  a  v  a 2s . c  o  m*/
    int dx;
    int dy;
    int action = ev.getAction();
    switch (action) {
    case MotionEvent.ACTION_DOWN:
        mLastX = (int) ev.getX();
        mLastY = (int) ev.getY();
        break;
    case MotionEvent.ACTION_MOVE:
        if (!isSwipeEnable())
            break;
        int disX = (int) (mLastX - ev.getX());
        int disY = (int) (mLastY - ev.getY());
        if (!mDragging && Math.abs(disX) > mScaledTouchSlop && Math.abs(disX) > Math.abs(disY)) {
            mDragging = true;
        }
        if (mDragging) {
            if (mSwipeCurrentHorizontal == null || shouldResetSwipe) {
                if (disX < 0) {
                    if (mSwipeLeftHorizontal != null)
                        mSwipeCurrentHorizontal = mSwipeLeftHorizontal;
                    else
                        mSwipeCurrentHorizontal = mSwipeRightHorizontal;
                } else {
                    if (mSwipeRightHorizontal != null)
                        mSwipeCurrentHorizontal = mSwipeRightHorizontal;
                    else
                        mSwipeCurrentHorizontal = mSwipeLeftHorizontal;
                }
            }
            scrollBy(disX, 0);
            mLastX = (int) ev.getX();
            mLastY = (int) ev.getY();
            shouldResetSwipe = false;
        }
        break;
    case MotionEvent.ACTION_UP:
        dx = (int) (mDownX - ev.getX());
        dy = (int) (mDownY - ev.getY());
        mDragging = false;
        mVelocityTracker.computeCurrentVelocity(1000, mScaledMaximumFlingVelocity);
        int velocityX = (int) mVelocityTracker.getXVelocity();
        int velocity = Math.abs(velocityX);
        if (velocity > mScaledMinimumFlingVelocity) {
            if (mSwipeCurrentHorizontal != null) {
                int duration = getSwipeDuration(ev, velocity);
                if (mSwipeCurrentHorizontal instanceof SwipeRightHorizontal) {
                    if (velocityX < 0) {
                        smoothOpenMenu(duration);
                    } else {
                        smoothCloseMenu(duration);
                    }
                } else {
                    if (velocityX > 0) {
                        smoothOpenMenu(duration);
                    } else {
                        smoothCloseMenu(duration);
                    }
                }
                ViewCompat.postInvalidateOnAnimation(this);
            }
        } else {
            judgeOpenClose(dx, dy);
        }
        mVelocityTracker.clear();
        mVelocityTracker.recycle();
        mVelocityTracker = null;
        if (Math.abs(mDownX - ev.getX()) > mScaledTouchSlop || Math.abs(mDownY - ev.getY()) > mScaledTouchSlop
                || isMenuOpen()) {
            ev.setAction(MotionEvent.ACTION_CANCEL);
            super.onTouchEvent(ev);
            return true;
        }
        break;
    case MotionEvent.ACTION_CANCEL:
        mDragging = false;
        if (!mScroller.isFinished()) {
            mScroller.abortAnimation();
        } else {
            dx = (int) (mDownX - ev.getX());
            dy = (int) (mDownY - ev.getY());
            judgeOpenClose(dx, dy);
        }
        break;
    }
    return super.onTouchEvent(ev);
}

From source file:com.adguard.android.commons.BrowserUtils.java

public static void showBrowserInstallDialog(final Context context) {
    // Touch listener for changing colors of CardViews
    View.OnTouchListener touchListener = new View.OnTouchListener() {
        @Override/*w  ww .j ava  2s . c o  m*/
        public boolean onTouch(View v, MotionEvent event) {
            int action = event.getActionMasked();
            if (action == MotionEvent.ACTION_DOWN) {
                ((CardView) v).setCardBackgroundColor(R.color.card_view_background_pressed);
            } else if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL
                    || action == MotionEvent.ACTION_OUTSIDE) {
                ((CardView) v).setCardBackgroundColor(R.color.white);
            }
            return false;
        }
    };

    final LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    final View dialogLayout = inflater.inflate(R.layout.select_browser_dialog, null);
    AlertDialog.Builder builder = new AlertDialog.Builder(context, R.style.AlertDialog);
    builder.setNegativeButton(android.R.string.cancel, null);
    builder.setView(dialogLayout);
    final AlertDialog dialog = builder.create();

    View cardView = dialogLayout.findViewById(R.id.browser_yandex);
    cardView.setOnTouchListener(touchListener);
    cardView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ActivityUtils.startMarket(context, YANDEX_BROWSER_PACKAGE, REFERRER);
            dialog.dismiss();
        }
    });

    cardView = dialogLayout.findViewById(R.id.browser_samsung);
    cardView.setOnTouchListener(touchListener);
    cardView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ActivityUtils.startMarket(context, SAMSUNG_BROWSER_PACKAGE, null);
            dialog.dismiss();
        }
    });

    dialog.show();
}

From source file:com.android.gallery3d.v5.filtershow.category.CategoryView.java

@Override
public boolean onTouchEvent(MotionEvent event) {
    boolean ret = super.onTouchEvent(event);
    FilterShowActivity activity = (FilterShowActivity) getContext();

    if (event.getActionMasked() == MotionEvent.ACTION_UP) {
        activity.startTouchAnimation(this, event.getX(), event.getY());
    }/*from   w ww.  j  a  va 2s .co  m*/
    if (!canBeRemoved()) {
        return ret;
    }
    if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {
        mStartTouchY = event.getY();
        mStartTouchX = event.getX();
    }
    if (event.getActionMasked() == MotionEvent.ACTION_UP) {
        setTranslationX(0);
        setTranslationY(0);
    }
    if (event.getActionMasked() == MotionEvent.ACTION_MOVE) {
        float delta = event.getY() - mStartTouchY;
        if (getOrientation() == CategoryView.VERTICAL) {
            delta = event.getX() - mStartTouchX;
        }
        if (Math.abs(delta) > mDeleteSlope) {
            activity.setHandlesSwipeForView(this, mStartTouchX, mStartTouchY);
        }
    }
    return true;
}

From source file:am.widget.scalerecyclerview.ScaleRecyclerView.java

@SuppressLint("ClickableViewAccessibility")
@Override/*from w  ww.  j a v a2s  . c om*/
public boolean onTouchEvent(MotionEvent e) {
    if (!mScaleEnable)
        return super.onTouchEvent(e);
    boolean superResult = false;
    // 
    if (e.getPointerCount() >= 2) {
        // 
        if (!mScaleBegin) {
            // ACTION_UP
            final int action = e.getAction();
            e.setAction(MotionEvent.ACTION_UP);
            setForceInterceptDispatchOnScrollStateChanged(true);
            superResult = super.onTouchEvent(e);
            setForceInterceptDispatchOnScrollStateChanged(false);
            e.setAction(action);
            mScaleBegin = true;
        }
        superResult = mScaleGestureDetector.onTouchEvent(e) || superResult;
    } else {
        // ?
        final int action = e.getAction();
        boolean dispatch = false;
        if (action == MotionEvent.ACTION_UP && getScrollState() == SCROLL_STATE_IDLE) {
            dispatch = true;
        }
        if (mScaleBegin) {
            mScaleBegin = false;
            // ?ACTION_DOWN
            e.setAction(MotionEvent.ACTION_DOWN);
            super.onInterceptTouchEvent(e);
            e.setAction(action);
        }
        superResult = super.onTouchEvent(e);
        if (dispatch) {
            dispatchOnScrollStateChanged(SCROLL_STATE_IDLE);
        }
    }
    return superResult;
}

From source file:com.android.documentsui.DocumentsActivity.java

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);

    mRoots = DocumentsApplication.getRootsCache(this);

    setResult(Activity.RESULT_CANCELED);
    setContentView(R.layout.activity);/*from  w  ww  .j  ava 2 s  . c o  m*/

    final Resources res = getResources();
    mShowAsDialog = res.getBoolean(R.bool.show_as_dialog);

    if (mShowAsDialog) {
        // backgroundDimAmount from theme isn't applied; do it manually
        final WindowManager.LayoutParams a = getWindow().getAttributes();
        a.dimAmount = 0.6f;
        getWindow().setAttributes(a);

        getWindow().setFlags(0, WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN);
        getWindow().setFlags(~0, WindowManager.LayoutParams.FLAG_DIM_BEHIND);

        // Inset ourselves to look like a dialog
        final Point size = new Point();
        getWindowManager().getDefaultDisplay().getSize(size);

        final int width = (int) res.getFraction(R.dimen.dialog_width, size.x, size.x);
        final int height = (int) res.getFraction(R.dimen.dialog_height, size.y, size.y);
        final int insetX = (size.x - width) / 2;
        final int insetY = (size.y - height) / 2;

        final Drawable before = getWindow().getDecorView().getBackground();
        final Drawable after = new InsetDrawable(before, insetX, insetY, insetX, insetY);
        getWindow().getDecorView().setBackground(after);

        // Dismiss when touch down in the dimmed inset area
        getWindow().getDecorView().setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    final float x = event.getX();
                    final float y = event.getY();
                    if (x < insetX || x > v.getWidth() - insetX || y < insetY || y > v.getHeight() - insetY) {
                        finish();
                        return true;
                    }
                }
                return false;
            }
        });

    } else {
        // Non-dialog means we have a drawer
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.drawable.ic_drawer_glyph,
                R.string.drawer_open, R.string.drawer_close);

        mDrawerLayout.setDrawerListener(mDrawerListener);
        mDrawerLayout.setDrawerShadow(R.drawable.ic_drawer_shadow, GravityCompat.START);

        mRootsContainer = findViewById(R.id.container_roots);
    }

    mDirectoryContainer = (DirectoryContainerView) findViewById(R.id.container_directory);

    if (icicle != null) {
        mState = icicle.getParcelable(EXTRA_STATE);
    } else {
        buildDefaultState();
    }

    // Hide roots when we're managing a specific root
    if (mState.action == ACTION_MANAGE) {
        if (mShowAsDialog) {
            findViewById(R.id.dialog_roots).setVisibility(View.GONE);
        } else {
            mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
        }
    }

    if (mState.action == ACTION_CREATE) {
        final String mimeType = getIntent().getType();
        final String title = getIntent().getStringExtra(Intent.EXTRA_TITLE);
        SaveFragment.show(getFragmentManager(), mimeType, title);
    }

    if (mState.action == ACTION_GET_CONTENT) {
        final Intent moreApps = new Intent(getIntent());
        moreApps.setComponent(null);
        moreApps.setPackage(null);
        RootsFragment.show(getFragmentManager(), moreApps);
    } else if (mState.action == ACTION_OPEN || mState.action == ACTION_CREATE) {
        RootsFragment.show(getFragmentManager(), null);
    }

    if (!mState.restored) {
        if (mState.action == ACTION_MANAGE) {
            final Uri rootUri = getIntent().getData();
            new RestoreRootTask(rootUri).executeOnExecutor(getCurrentExecutor());
        } else {
            new RestoreStackTask().execute();
        }
    } else {
        onCurrentDirectoryChanged(ANIM_NONE);
    }
}

From source file:com.tarun.smartwomen.WebViewDemoActivity.java

@SuppressLint({ "JavascriptInterface", "SetJavaScriptEnabled" })
@Override// ww w.  j  ava2s .c  o  m
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.mainn);

    Intent intent = getIntent();
    reg = intent.getBooleanExtra("reg", false);

    i = (ImageView) findViewById(R.id.hj);

    try {

        big = new GifAnimationDrawable(getResources().openRawResource(R.raw.anim2));
        // big.setOneShot(true);
        android.util.Log.v("GifAnimationDrawable", "===>Four");
    } catch (IOException ioe) {

    }

    i.setImageDrawable(big);
    big.setVisible(true, true);

    sp = getSharedPreferences("your_prefs", Activity.MODE_PRIVATE);
    // historyStack = new LinkedList<Link>();
    webview = (WebView) findViewById(R.id.webkit);

    WebSettings webSettings = webview.getSettings();
    webSettings.setJavaScriptEnabled(true);

    webSettings.setDomStorageEnabled(true);
    // webview.addJavascriptInterface(new WebViewDemoActivity(), "Android");
    // webview.getSettings().setJavaScriptEnabled(true);

    webview.getSettings().setBuiltInZoomControls(true);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) {
        // webview.getSettings().setPluginState(PluginState.ON);
        // webview.getSettings().setJavaScriptEnabled(true);
    } else {
        // IMPORTANT!! this method is no longer available since Android 4.3
        // so the code doesn't compile anymore
        // webview.getSettings().setPluginsEnabled(true);
    }

    // Internet

    ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(
            Context.CONNECTIVITY_SERVICE);
    connected = false;
    if ((null != connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE) && connectivityManager
            .getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTED)
            || (null != connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI) && connectivityManager
                    .getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.CONNECTED)) {
        // we are connected to a network
        connected = true;
    }

    if (connected == false) {
        /*
         * Toast.makeText(Rss.this,
         * "Connect to internet and Restart Application",
         * Toast.LENGTH_SHORT).show();
         */
        webview.setVisibility(View.INVISIBLE);

        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(WebViewDemoActivity.this);

        // alertDialogBuilder.setTitle("Please connect to Internet");
        alertDialogBuilder.setMessage(
                "In order to provide the freshest recipes and juicing information this app must be connected to the internet, please check your internet settings");
        // set positive button: Yes message

        alertDialogBuilder.setNegativeButton("Ok", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                // cancel the alert box and put a Toast to the user

                startActivityForResult(new Intent(android.provider.Settings.ACTION_SETTINGS), 0);
            }
        });
        AlertDialog alertDialog = alertDialogBuilder.create();
        // show alert
        alertDialog.show();

    }

    // downloads
    // webview.setDownloadListener(new CustomDownloadListener());

    webview.setWebViewClient(new CustomWebViewClient());

    webview.setWebChromeClient(new WebChromeClient() {
        @Override
        public void onProgressChanged(WebView view, int progress) {

        }

        @Override
        public void onReceivedTitle(WebView view, String title) {
        }

        @Override
        public void onReceivedIcon(WebView view, Bitmap icon) {

        }

    });

    // http://stackoverflow.com/questions/2083909/android-webview-refusing-user-input
    webview.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_UP:
                if (!v.hasFocus()) {
                    v.requestFocus();
                }
                break;
            }
            return false;
        }

    });

    if (reg == true) {
        if (Locale.getDefault().getLanguage().equals("es")) {

            webview.loadUrl("http://om-msmartwoman.com/member/register");
            // webview.loadUrl("http://live-juice-guru.gotpantheon.com/user/login");
        } else {
            webview.loadUrl("http://om-msmartwoman.com/member/register");
        }

        webview.requestFocus();
    } else {
        uhdj = sp.getString("your_int_key", "0");

        Log.e("Url is here ..............................", uhdj);
        // Welcome page loaded from assets directory
        if (Locale.getDefault().getLanguage().equals("es")) {

            webview.loadUrl(uhdj);
            // webview.loadUrl("http://live-juice-guru.gotpantheon.com/user/login");
        } else {
            webview.loadUrl(uhdj);
        }

        webview.requestFocus();
    }

}

From source file:com.alex.smartwomanmiddleeastfem.WebViewDemoActivity.java

@SuppressLint({ "JavascriptInterface", "SetJavaScriptEnabled" })
@Override//w  w  w. j  a v a  2 s . c om
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.mainn);

    Intent intent = getIntent();
    reg = intent.getBooleanExtra("reg", false);

    i = (ImageView) findViewById(R.id.hj);

    try {

        big = new GifAnimationDrawable(getResources().openRawResource(R.raw.anim2));
        // big.setOneShot(true);
        android.util.Log.v("GifAnimationDrawable", "===>Four");
    } catch (IOException ioe) {

    }

    i.setImageDrawable(big);
    big.setVisible(true, true);

    sp = getSharedPreferences("your_prefs", Activity.MODE_PRIVATE);
    // historyStack = new LinkedList<Link>();
    webview = (WebView) findViewById(R.id.webkit);

    WebSettings webSettings = webview.getSettings();
    webSettings.setJavaScriptEnabled(true);

    webSettings.setDomStorageEnabled(true);
    // webview.addJavascriptInterface(new WebViewDemoActivity(), "Android");
    // webview.getSettings().setJavaScriptEnabled(true);

    webview.getSettings().setBuiltInZoomControls(true);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) {
        // webview.getSettings().setPluginState(PluginState.ON);
        // webview.getSettings().setJavaScriptEnabled(true);
    } else {
        // IMPORTANT!! this method is no longer available since Android 4.3
        // so the code doesn't compile anymore
        // webview.getSettings().setPluginsEnabled(true);
    }

    // Internet

    ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(
            Context.CONNECTIVITY_SERVICE);
    connected = false;
    if ((null != connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE) && connectivityManager
            .getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTED)
            || (null != connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI) && connectivityManager
                    .getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.CONNECTED)) {
        // we are connected to a network
        connected = true;
    }

    if (connected == false) {
        /*
         * Toast.makeText(Rss.this,
         * "Connect to internet and Restart Application",
         * Toast.LENGTH_SHORT).show();
         */
        webview.setVisibility(View.INVISIBLE);

        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(WebViewDemoActivity.this);

        // alertDialogBuilder.setTitle("Please connect to Internet");
        alertDialogBuilder.setMessage(
                "Through our app offers some offline features, in order to stay actively connected to the SmartWoman community in realtime, you need an internet connection, tap here to check your settings or wait until you have connection.");
        // set positive button: Yes message

        alertDialogBuilder.setNegativeButton("Ok", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                // cancel the alert box and put a Toast to the user

                startActivityForResult(new Intent(android.provider.Settings.ACTION_SETTINGS), 0);
            }
        });
        AlertDialog alertDialog = alertDialogBuilder.create();
        // show alert
        alertDialog.show();

    }

    // downloads
    // webview.setDownloadListener(new CustomDownloadListener());

    webview.setWebViewClient(new CustomWebViewClient());

    webview.setWebChromeClient(new WebChromeClient() {
        @Override
        public void onProgressChanged(WebView view, int progress) {

        }

        @Override
        public void onReceivedTitle(WebView view, String title) {
        }

        @Override
        public void onReceivedIcon(WebView view, Bitmap icon) {

        }

    });

    // http://stackoverflow.com/questions/2083909/android-webview-refusing-user-input
    webview.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_UP:
                if (!v.hasFocus()) {
                    v.requestFocus();
                }
                break;
            }
            return false;
        }

    });

    if (reg == true) {
        if (Locale.getDefault().getLanguage().equals("es")) {

            webview.loadUrl("http://www.qa-msmartwoman.com/member/register");
            // webview.loadUrl("http://live-juice-guru.gotpantheon.com/user/login");
        } else {
            webview.loadUrl("http://www.qa-msmartwoman.com/member/register");
        }

        webview.requestFocus();
    } else {
        uhdj = sp.getString("your_int_key", "0");

        //   Log.e("Url is here ..............................", uhdj);
        // Welcome page loaded from assets directory
        if (Locale.getDefault().getLanguage().equals("es")) {

            webview.loadUrl(uhdj);
            // webview.loadUrl("http://live-juice-guru.gotpantheon.com/user/login");
        } else {
            webview.loadUrl(uhdj);
        }

        webview.requestFocus();
    }

}

From source file:co.paulburke.android.textviewpager.TextViewPagerIndicator.java

public void setViewPager(ViewPager viewPager) {
    if (mViewPager == viewPager) {
        return;/*from  ww w .j ava  2 s .  co  m*/
    }
    if (mViewPager != null) {
        // Clear us from the old pager.
        mViewPager.setOnPageChangeListener(null);
    }
    if (viewPager.getAdapter() == null) {
        throw new IllegalStateException("ViewPager does not have adapter instance.");
    }
    mViewPager = (DirectionalViewPager) viewPager;
    mViewPager.setOnPageChangeListener(this);
    mViewPager.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                show();
            }
            return false;
        }
    });
    invalidate();
    post(new Runnable() {
        @Override
        public void run() {
            if (mFades) {
                post(mFadeRunnable);
            }
        }
    });
}

From source file:com.android.hiparker.lierda_light.view.viewpagerindicator.LinePageIndicator.java

public boolean onTouchEvent(android.view.MotionEvent ev) {
    if (super.onTouchEvent(ev)) {
        return true;
    }//from  ww w .  jav a2 s  .c om
    if ((mViewPager == null) || (mViewPager.getAdapter().getCount() == 0)) {
        return false;
    }

    final int action = ev.getAction() & MotionEventCompat.ACTION_MASK;
    switch (action) {
    case MotionEvent.ACTION_DOWN:
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        mLastMotionX = ev.getX();
        break;

    case MotionEvent.ACTION_MOVE: {
        final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        final float x = MotionEventCompat.getX(ev, activePointerIndex);
        final float deltaX = x - mLastMotionX;

        if (!mIsDragging) {
            if (Math.abs(deltaX) > mTouchSlop) {
                mIsDragging = true;
            }
        }

        if (mIsDragging) {
            mLastMotionX = x;
            if (mViewPager.isFakeDragging() || mViewPager.beginFakeDrag()) {
                mViewPager.fakeDragBy(deltaX);
            }
        }

        break;
    }

    case MotionEvent.ACTION_CANCEL:
    case MotionEvent.ACTION_UP:
        if (!mIsDragging) {
            final int count = mViewPager.getAdapter().getCount();
            final int width = getWidth();
            final float halfWidth = width / 2f;
            final float sixthWidth = width / 6f;

            if ((mCurrentPage > 0) && (ev.getX() < halfWidth - sixthWidth)) {
                if (action != MotionEvent.ACTION_CANCEL) {
                    mViewPager.setCurrentItem(mCurrentPage - 1);
                }
                return true;
            } else if ((mCurrentPage < count - 1) && (ev.getX() > halfWidth + sixthWidth)) {
                if (action != MotionEvent.ACTION_CANCEL) {
                    mViewPager.setCurrentItem(mCurrentPage + 1);
                }
                return true;
            }

        }

        mIsDragging = false;
        mActivePointerId = INVALID_POINTER;
        if (mViewPager.isFakeDragging())
            mViewPager.endFakeDrag();
        break;

    case MotionEventCompat.ACTION_POINTER_DOWN: {
        final int index = MotionEventCompat.getActionIndex(ev);
        mLastMotionX = MotionEventCompat.getX(ev, index);
        mActivePointerId = MotionEventCompat.getPointerId(ev, index);
        break;
    }

    case MotionEventCompat.ACTION_POINTER_UP:
        final int pointerIndex = MotionEventCompat.getActionIndex(ev);
        final int pointerId = MotionEventCompat.getPointerId(ev, pointerIndex);
        if (pointerId == mActivePointerId) {
            final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
            mActivePointerId = MotionEventCompat.getPointerId(ev, newPointerIndex);
        }
        mLastMotionX = MotionEventCompat.getX(ev, MotionEventCompat.findPointerIndex(ev, mActivePointerId));
        break;
    }

    return true;
}

From source file:cn.org.eshow.framwork.view.sliding.AbSlidingPageView.java

/**
 * View.//from ww w.j av  a2 s.  c o m
 *
 * @param view the view
 */
public void addContentView(View view) {
    addView(view, 0, getLayoutParams());
    view.setOnFocusChangeListener(new OnFocusChangeListener() {

        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus) {
                showPrevious();
            }

        }
    });

    view.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (MotionEvent.ACTION_DOWN == event.getAction()) {
                showPrevious();
            }
            return false;
        }
    });
}