onSwipe(int direction) not working

2019-06-09 11:22发布

问题:

I can't override the onSwipe() method. The error is "The method onSwipe(int) of type Adds must override or implement a supertype method". Can anyone tell me what I did wrong?. I want to navigate between activities using swipe gesture. Is there any other way to do it? If so please explain. Do I have to import any more packages?

package com.mahavega.qcdemo;

import com.mahavega.qcdemo.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

public class Adds extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ads);
        TextView tv1 = (TextView) findViewById(R.id.textView1);
        tv1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                startActivity(new Intent(Adds.this, Ads2.class));
            }
        });
        ImageView im1 = (ImageView) findViewById(R.id.imageView1);
        im1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                startActivity(new Intent(Adds.this, Real.class));
            }
        });
    }
    @Override
    public void onSwipe(int direction) {
        Intent intent = new Intent();

        switch (direction) {
        case SimpleGestureFilter.SWIPE_RIGHT:
            intent.setClass(this, Ads2.class);
            break;

        case SimpleGestureFilter.SWIPE_LEFT:
            intent.setClass(this, Ads3.class);
            break;
        }

        startActivity(intent);
    }
}

回答1:

your activity should implement SimpleGestureListener to be able to recieve gesture events.

public class Adds extends Activity implements OnGestureListener


回答2:

Your error reads from the fact that Activity does not have an onSwipe(int) method. So the error is stating that you cannot override a method that has no super method. Also as @sokie said, check out the OnGestureListener from the link he added.As for swiping gesture used to start new activities, when swiping left to right, override call onBackPressed (like you are going back) and on swipe right to left start new activity. Although this means you have to create the gesture listener in each of your activities.