Skip to content Skip to sidebar Skip to footer

Swipelistener On Listview And Clicklistener On Listview's Items

This is my old question. However this time I have provided my code as well. I have a ListView with different types of rows. The row may contain text, image, video or something else

Solution 1:

Thats Not Possible with both onClickListener for listitem and SwipeListener for List,because it gets Ambiguity between which View to Consider on touch

You Use OnSwipeTouchListener which implements onTouchListener

OnSwipeTouchListener.java

import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

publicclassOnSwipeTouchListenerimplementsOnTouchListener {

    privatefinal GestureDetector gestureDetector;

    publicOnSwipeTouchListener(Context ctx){
        gestureDetector = newGestureDetector(ctx, newGestureListener());
    }

    privatefinalclassGestureListenerextendsSimpleOnGestureListener {

        privatestaticfinalintSWIPE_THRESHOLD=100;
        privatestaticfinalintSWIPE_VELOCITY_THRESHOLD=100;

        @OverridepublicbooleanonDown(MotionEvent e) {
            returntrue;
        }

        @OverridepublicbooleanonFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            booleanresult=false;
            try {
                floatdiffY= e2.getY() - e1.getY();
                floatdiffX= e2.getX() - e1.getX();
                if (Math.abs(diffX) > Math.abs(diffY)) {
                    if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                        if (diffX > 0) {
                            onSwipeRight();
                        } else {
                            onSwipeLeft();
                        }
                    }
                    result = true;
                } 
                elseif (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
                        if (diffY > 0) {
                            onSwipeBottom();
                        } else {
                            onSwipeTop();
                        }
                    }
                    result = true;

            } catch (Exception exception) {
                exception.printStackTrace();
            }
            return result;
        }
    }

    publicvoidonSwipeRight() {
    }

    publicvoidonSwipeLeft() {
    }

    publicvoidonSwipeTop() {
    }

    publicvoidonSwipeBottom() {
    }
}

And use OnSwipeTouchListener on listitem

  listitem.setOnTouchListener(new OnSwipeTouchListener() {

        publicvoidonSwipeLeft() {
            //stuff to do list view left swipe 
            Toast.makeText(MyActivity.this, "left", Toast.LENGTH_SHORT).show();
        }


        public boolean onTouch(View v, MotionEvent event) {
            //stuff to do on list item clickreturn gestureDetector.onTouchEvent(event);
        }
    });

}); //to get click events 

Solution 2:

I know this is an old question but still for future search here is a better solution:

Try to use RecyclerView instead of ListView and add OnItemTouchListener of recyclerview's item.

recyclerView.addOnItemTouchListener(
            new RecyclerItemClickListener(
                    this,
                    new RecyclerItemClickListener.OnItemClickListener() {
                        @Override
                        public void onItemClick(View view, int position) {
                            // Your code here for item on click event...
                        }
                    },
                    runnableSwipeLeftToRight,
                    runnableSwipeRightToLeft)
    );

Here are two methods which will execute on appropriate swipes:

Runnable runnableSwipeRightToLeft = new Runnable() {
    publicvoidrun() {
        // Your Code here...
    }
};


Runnable runnableSwipeLeftToRight = new Runnable() {
    publicvoidrun() {
        // Your code here...
    }
};

And here is class code for touch and swipe detection:

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;

publicclassRecyclerItemClickListenerimplementsRecyclerView.OnItemTouchListener, RecyclerView.OnLongClickListener {

private OnItemClickListener mListener;
GestureDetector gestureDetector;
Runnable runnableSwipeLeftToRight, runnableSwipeRightToLeft;

privatestaticfinalStringTAG="RecyclerItemClickListen";
privatestaticfinalintSWIPE_MIN_DISTANCE=120;
privatestaticfinalintSWIPE_MAX_OFF_PATH=200;
privatestaticfinalintSWIPE_THRESHOLD_VELOCITY=200;

@OverridepublicbooleanonLongClick(View view) {
    returnfalse;
}

publicinterfaceOnItemClickListener {
    publicvoidonItemClick(View view, int position);
}

GestureDetector mGestureDetector;

publicRecyclerItemClickListener(Context context, OnItemClickListener listener, Runnable runnableSwipeLeftToRight, Runnable runnableSwipeRightToLeft) {
    this.mListener = listener;
    this.runnableSwipeLeftToRight = runnableSwipeLeftToRight;
    this.runnableSwipeRightToLeft = runnableSwipeRightToLeft;

    mGestureDetector = newGestureDetector(context, newGestureDetector.SimpleOnGestureListener() {
        @OverridepublicbooleanonSingleTapUp(MotionEvent e) {
            returntrue;
        }
    });

    gestureDetector = newGestureDetector(context, newGestureDetector.OnGestureListener() {
        @OverridepublicbooleanonDown(MotionEvent motionEvent) {
            returnfalse;
        }

        @OverridepublicvoidonShowPress(MotionEvent motionEvent) { }

        @OverridepublicbooleanonSingleTapUp(MotionEvent motionEvent) {
            returnfalse;
        }

        @OverridepublicbooleanonScroll(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) { returnfalse; }

        @OverridepublicvoidonLongPress(MotionEvent motionEvent) { }

        @OverridepublicbooleanonFling(MotionEvent e1, MotionEvent e2, float velocityX, float v1) {
            try {
                floatdiffAbs= Math.abs(e1.getY() - e2.getY());
                floatdiff= e1.getX() - e2.getX();

                if (diffAbs > SWIPE_MAX_OFF_PATH) {
                    returnfalse;
                }

                // Left swipeif (diff > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    onLeftSwipe();

                    // Right swipe
                } elseif (-diff > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    onRightSwipe();
                }
            } catch (Exception e) {
                Log.e("YourActivity", "Error on gestures");
            }
            returnfalse;
        }
    });
}

privatevoidonLeftSwipe() {
    Log.d(TAG, "onLeftSwipe: ");
    runnableSwipeRightToLeft.run();
}

privatevoidonRightSwipe() {
    Log.d(TAG, "onRightSwipe:");
    runnableSwipeLeftToRight.run();
}

@OverridepublicbooleanonInterceptTouchEvent(RecyclerView view, MotionEvent e) {
    ViewchildView= view.findChildViewUnder(e.getX(), e.getY());
    if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e)) {
        mListener.onItemClick(childView, view.getChildAdapterPosition(childView));
    }
    return gestureDetector.onTouchEvent(e);
}

@OverridepublicvoidonTouchEvent(RecyclerView view, MotionEvent motionEvent) {
}

@OverridepublicvoidonRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

}

}

Hope this will help you.

Post a Comment for "Swipelistener On Listview And Clicklistener On Listview's Items"