rambler-digital-solutions / swipe-layout-android

Android UI widget
324 stars 68 forks source link

setOnClickListener fired during swipe #34

Open truXton222 opened 6 years ago

truXton222 commented 6 years ago

Is possible to avoid setOnClickListener fired during the swipe gesture?

javichaques commented 5 years ago

Hi,

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

Thanks

truXton222 commented 5 years ago

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 commented 5 years ago

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 commented 4 years ago

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