Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

setOnClickListener fired during swipe #34

Open
truXton222 opened this issue Sep 13, 2018 · 4 comments
Open

setOnClickListener fired during swipe #34

truXton222 opened this issue Sep 13, 2018 · 4 comments

Comments

@truXton222
Copy link

Is possible to avoid setOnClickListener fired during the swipe gesture?

@javichaques
Copy link
Contributor

Hi,

I have the same problem.
@truXton222 How did you resolve it?

Thanks

@truXton222
Copy link
Author

Hi,

I have the same problem.
@truXton222 How did you resolve it?

Thanks

nope,

i tried many implementation, but none of these prevent the onClick during swipe.

if u have better luck please update me.

Good luck :)

@MuhammadMuzammilSharif
Copy link

MuhammadMuzammilSharif commented Feb 10, 2019

found solution:

replace SwipeLayout.java's onTouchEvent function with

@Override
    public boolean onTouchEvent(MotionEvent event) {
        boolean defaultResult = super.onTouchEvent(event);
        if (!isSwipeEnabled()) {
            return defaultResult;
        }

        switch (event.getActionMasked()) {
            case MotionEvent.ACTION_DOWN:
                onTouchBegin(event);
                break;

            case MotionEvent.ACTION_MOVE:
                isMoved = true;
                if (touchState == TOUCH_STATE_WAIT) {
                    float dx = Math.abs(event.getX() - touchX);
                    float dy = Math.abs(event.getY() - touchY);

                    boolean isLeftToRight = (event.getX() - touchX) > 0;

                    if (((isLeftToRight && !leftSwipeEnabled) || (!isLeftToRight && !rightSwipeEnabled))
                            &&
                            getOffset() == 0) {

                        return defaultResult;
                    }

                    if (dx >= touchSlop || dy >= touchSlop) {
                        touchState = dy == 0 || dx / dy > 1f ? TOUCH_STATE_SWIPE : TOUCH_STATE_SKIP;
                        if (touchState == TOUCH_STATE_SWIPE) {
                            requestDisallowInterceptTouchEvent(true);

                            hackParents();

                            if (swipeListener != null)
                                swipeListener.onBeginSwipe(this, event.getX() > touchX);
                        }
                    }
                }
                break;

            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_UP:
                if (touchState == TOUCH_STATE_SWIPE) {
                    unHackParents();
                    requestDisallowInterceptTouchEvent(false);
                }
                touchState = TOUCH_STATE_WAIT;
                break;
        }

        if (event.getActionMasked() != MotionEvent.ACTION_MOVE || touchState == TOUCH_STATE_SWIPE) {
            dragHelper.processTouchEvent(event);
        }
        if (isMoved || event.getActionMasked() == MotionEvent.ACTION_DOWN) {
            isMoved = false;
            return true;
        } else {
            if (!isMoved && event.getActionMasked() == MotionEvent.ACTION_UP) {
                return defaultResult;
            } else {
                return true;
            }
        }
    }

and create a variable in SwipeLayout.java class

    private boolean isMoved = false;

@salonibisht
Copy link

salonibisht commented Dec 20, 2019

I have also found a solution hope it will help you:
you have to set Swipe Listener and Click Listeners Separately.
for example your xml is like this:

 <ru.rambler.libs.swipe_layout.SwipeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:id="@+id/swipe_layout"
        android:paddingBottom="@dimen/dp_1"
 <RelativeLayout
            android:id="@+id/view_background"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingStart="@dimen/dp_10"
            android:paddingTop="@dimen/dp_5"
            android:paddingEnd="@dimen/dp_10"
            android:paddingBottom="@dimen/dp_5"
            app:bring_to_clamp="200dp"
            app:clamp="parent"
            app:gravity="right"
            app:sticky="@dimen/dp_100"
            android:background="@color/colorRedPink">
      </RelativeLayout>
 <RelativeLayout
            android:id="@+id/view_foreground"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingStart="@dimen/dp_10"
            android:paddingTop="@dimen/dp_5"
            android:paddingEnd="@dimen/dp_10"
            android:paddingBottom="@dimen/dp_5"
            app:bring_to_clamp="200dp"
            app:clamp="parent"
            app:gravity="right"
            app:sticky="@dimen/dp_100"
            android:background="@color/colorRedPink">
        </RelativeLayout>
       </ru.rambler.libs.swipe_layout.SwipeLayout>

Now you can set swipelistner on swipe layout with id swipe_layout and click listener on other views with id view_background and view_foreground

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants