16

Ok. This thing almost drives me crazy.

Some days ago I could implement the code below and the toast was successfully called when I tried to swipe right or left.

However, now it can't be called, because the e1 is always null!

How could this happen? I try this code on the emulator, but a user reports me, that it also doesn't work on real device.

public class SwipeGestureListener extends GestureDetector.SimpleOnGestureListener{

    private static final int SWIPE_MIN_DISTANCE = 150;
    private static final int SWIPE_MAX_OFF_PATH = 100;

    private static final int SWIPE_THRESHOLD_VELOCITY = 100;
    private final Activity activity;
    protected MotionEvent mLastOnDownEvent = null;

    public SwipeGestureListener(Activity activity) {
        this.activity = activity;
    }

    @Override
    public boolean onDown(MotionEvent e) {
        mLastOnDownEvent = e;
        System.out.println(e);
        return super.onDown(e);
    }

    @Override   
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        System.out.println(e1 + " " + e2);
        if (e1==null)
            e1 = mLastOnDownEvent;
        if (e1==null || e2==null)
            return false;

        float dX = e2.getX()-e1.getX();
        float dY = e2.getY()-e1.getY();

        if (Math.abs(dY)<SWIPE_MAX_OFF_PATH && Math.abs(velocityX)>=SWIPE_THRESHOLD_VELOCITY && Math.abs(dX)>=SWIPE_MIN_DISTANCE ) {

            if (dX>0) {
                Toast.makeText(activity.getApplicationContext(), "Right Swipe", Toast.LENGTH_SHORT).show();
                activity.fetchPrevious();
            } else {
                Toast.makeText(activity.getApplicationContext(), "Left Swipe", Toast.LENGTH_SHORT).show();
                activity.fetchNext();
            }

            return true;
        } 

        return false;
    }

}

The code that implement the gesture:

final GestureDetector gdt = new GestureDetector(this, new SwipeGestureListener(this));
listview.setOnTouchListener(new OnTouchListener(){

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        gdt.onTouchEvent(event);
        return false;
        }

});
1
  • 1
    can anyone give a proper solution for this?i'm also facing same issue.
    – Jack
    Jun 22, 2017 at 7:18

4 Answers 4

1

I tried your code and indeed it didn't work, then I changed the GestureDetector to the newer/updated API "GestureDetectorCompat" and changed a few further things: Used the GestureDetector.OnGestureListener instead of the OnSimpleGestureListener, onDown() should return true and then it worked. I can't explain it to you, what was the exact problem. But the change lines seemed to be problematic. Maybe someone can explain the underlying problem. Have a look at the code, which is working:

public class MainActivity extends Activity {
   ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageView= (ImageView) findViewById(R.id.imageView1);

        final GestureDetectorCompat gdt = new GestureDetectorCompat(this, new SwipeGestureListener(this));
        imageView.setOnTouchListener(new OnTouchListener(){

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return gdt.onTouchEvent(event);
                }

        });

    }




    public class SwipeGestureListener implements GestureDetector.OnGestureListener{

        private static final int SWIPE_MIN_DISTANCE = 150;
        private static final int SWIPE_MAX_OFF_PATH = 100;

        private static final int SWIPE_THRESHOLD_VELOCITY = 100;
        private final Activity activity;
        protected MotionEvent mLastOnDownEvent = null;

        public SwipeGestureListener(Activity activity) {
            this.activity = activity;
    }

        @Override
        public boolean onDown(MotionEvent e) {
            mLastOnDownEvent = e;
            System.err.println("ondown");
            //System.out.println(e);
            return true;
        }

        @Override   
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            System.err.println("onFling");
            System.out.println(e1 + " " + e2);
            if (e1==null)
                e1 = mLastOnDownEvent;
            if (e1==null || e2==null)
                return false;

            float dX = e2.getX()-e1.getX();
            float dY = e2.getY()-e1.getY();

            if (Math.abs(dY)<SWIPE_MAX_OFF_PATH && Math.abs(velocityX)>=SWIPE_THRESHOLD_VELOCITY && Math.abs(dX)>=SWIPE_MIN_DISTANCE ) {

                if (dX>0) {
                    Toast.makeText(activity.getApplicationContext(), "Right Swipe", Toast.LENGTH_SHORT).show();
                   // activity.fetchPrevious();
                } else {
                    Toast.makeText(activity.getApplicationContext(), "Left Swipe", Toast.LENGTH_SHORT).show();
                   // activity.fetchNext();
                }

                return true;
            } 

            return false;
        }

        @Override
        public void onLongPress(MotionEvent e) {
            // TODO Auto-generated method stub

    }

        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { 
            // TODO Auto-generated method stub
            return false;
    }

        @Override
        public void onShowPress(MotionEvent e) {
            // TODO Auto-generated method stub

    }

       @Override
       public boolean onSingleTapUp(MotionEvent e) {
          // TODO Auto-generated method stub
           return false;
       }

       }

   }

Please try it!

4
  • Sorry G_J, it's still the same. e1 still gives me null value
    – Rendy
    Jun 30, 2013 at 16:45
  • That's really strange. Did you try it with a different View (not ListView)? The code above works absolutely flawlessly, when I register an ImageView to the OnTouchListener! Could you show the rest of your code?
    – G_J
    Jun 30, 2013 at 17:56
  • Yes I agree.. It works with the other view but not on one of my listview.. I will try to investigate the xml then.. Thanks for your help G_J
    – Rendy
    Jul 1, 2013 at 1:25
  • @G_J your idea is good but in my case, onDown() is never called, so I cannot intercept null value of e1. should onDown() be necessarily called when we swipe?
    – mangusta
    May 30, 2014 at 7:10
0

I had the same problem. If there is a onclicklistener etc. which are swiping items, you should remove that listeners. Then it will work. Thank you..

0

I had a similar problem and solved it by adding a FrameLayout along with needed View (I was using a GridView, but think it can work with a ListView or other view)

GestureListener is the same as yours. The layout:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout android:id="@+id/colors_grid_panel"
             xmlns:android="http://schemas.android.com/apk/res/android"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:background="@color/white"
             android:paddingLeft="@dimen/create_side_padding"
             android:paddingRight="@dimen/create_side_padding"
             android:visibility="gone">

    <GridView
        android:id="@+id/colors_grid"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:numColumns="7"
        android:paddingBottom="10dp"
        android:paddingTop="10dp"
        android:stretchMode="columnWidth"
        android:verticalSpacing="10dp"/>

    <FrameLayout
        android:id="@+id/stub"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</FrameLayout>

In code:

colorsGrid.setOnTouchListener(onSwipeTouchListener);
stub.setOnTouchListener(onSwipeTouchListener);
0

I had the same problem. Make sure all your views have add the same setOnTouchListener() method. If your touch place is outside view you will get null on e1.

My Example:

private View.OnTouchListener onTouchListener = new OnSwipeTouchListener(mActivity) {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            return super.onTouch(v, event);
        }

        @Override
        public void onSwipeLeft() {

        }

        @Override
        public void onSwipeRight() {
        }
    };


    @Override
    public View onCreateView(LayoutInflater inflater,
                      ViewGroup container, Bundle savedInstanceState) {

    //Inflate the layout for this fragment
    View v = inflater.inflate(R.layout.view, container, false);
    mListView = (ListView) v.findViewById(R.id.list);
    v.setFocusableInTouchMode(true);
    v.requestFocus();
    v.setOnTouchListener(onTouchListener);
    mListView.setOnTouchListener(onTouchListener);

    mAdapter = new OnlineAdapter(getActivity(), R.layout.row, onTouchListener);
    mListView.setAdapter(mAdapter);
    return v;

}

And my list adapter:

@Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        // something

            mImage = (ImageView) convertView.findViewById(R.id.image);
            mImage.setOnTouchListener(onTouchListener);
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.