سؤال

I am considering creating an Android app as part of a wider art project, and having no experience in Android development need to check if something is possible/feasible.

It requires a very simple interface, navigating between several (15 - 20) pages containing images or text. The navigation needs to be left/right and up/down swiping.

I have achieved a proof of concept (via copy/paste code from previous questions on the subject) in a very simple app using PagerAdapter/OnClickListener but this only does left/right switching.

Is there a way of using this for up/down navigation between pages? Any help/advice would be greatly appreciated,

import android.content.Context;
import android.content.Intent;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MyPagerAdapter extends PagerAdapter {


    public int getCount() {
        return 5;
    }


    @Override
    public Object instantiateItem(View container, int position) {
        Context context = container.getContext();
        LinearLayout layout = new LinearLayout(context);

        TextView textItem = new TextView(context);
        Button buttonItem = new Button(context);
        buttonItem.setText("Aceptar");
        buttonItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent("com.phone");

            }
        });


        switch (position) {
        case 0:
           textItem.setText("Photo 1 - begining");
           break;
        case 1:
           textItem.setText("Photo 2");
           break;
        case 2:
            textItem.setText("Photo 3");
            break;


case 3:
        textItem.setText("Photo 4");
        break;
    case 4:
        textItem.setText("Photo 5 - end");
        break;
    }
    layout.addView(textItem);
    ((ViewPager) container).addView(layout, 0); 
    return layout;
}
@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
    ((ViewPager) arg0).removeView((View) arg2);
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
    return arg0 == ((View) arg1);
}
@Override
public Parcelable saveState() {
    return null;
}
}

Here is the new code giving error

The method onTouch(View, MotionEvent) is undefined for the type Object

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

public class OnSwipeTouchListener implements OnTouchListener {

    private final GestureDetector gestureDetector = new GestureDetector(new GestureListener());

    public boolean onTouch(final View view, final MotionEvent motionEvent) {
        super.onTouch(view, motionEvent);
        return gestureDetector.onTouchEvent(motionEvent);
    }

    private final class GestureListener extends SimpleOnGestureListener {

        private static final int SWIPE_THRESHOLD = 100;
        private static final int SWIPE_VELOCITY_THRESHOLD = 100;

        @Override
        public boolean onDown(MotionEvent e) {
            return true;
        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            boolean result = false;
            try {
                float diffY = e2.getY() - e1.getY();
                float diffX = 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();
                        }
                    }
                } else {
                    if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
                        if (diffY > 0) {
                            onSwipeBottom();
                        } else {
                            onSwipeTop();
                        }
                    }
                }
            } catch (Exception exception) {
                exception.printStackTrace();
            }
            return result;
        }
    }

    public void onSwipeRight() {
    }

    public void onSwipeLeft() {
    }

    public void onSwipeTop() {
    }

    public void onSwipeBottom() {
    }
}
هل كانت مفيدة؟

المحلول

For some reason, I was thinking you had a runtime error, but no, you're plainly seeing an error message in Eclipse before you even tried to compile. Even if that code compiled, you have to actually apply your gesture detector against a layout container in order for it to do anything.

You're calling super.onTouch() in your OnSwipeTouchListener class, which is a subclass of Object. Object has no onTouch() method, and that's what the compiler is complaining about. That onTouch() method needs to go in your activity, not in your custom gesture listener.

I'm going to guess that whatever example you used, you got a bit confused with all the cutting and pasting.

Here's some very simple sample code that detects directional flings that demonstrates how to apply a custom gesture listener:

http://pcfandroid.wordpress.com/2011/07/17/swipe-with-android-android-tutorial/

That article is dead easy to follow, very clear and very simple.

نصائح أخرى

    public class Test extends Activity{

private GestureDetector gesturedetector = null;

View layout;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.test);

layout = (LinearLayout)findViewById(R.id.container);

gesturedetector = new GestureDetector(new MyGestureListener());

layout.setOnTouchListener(new OnTouchListener() {

@Override

public boolean onTouch(View v, MotionEvent event) {

gesturedetector.onTouchEvent(event);

return true;

}

});

}

public boolean dispatchTouchEvent(MotionEvent ev){

super.dispatchTouchEvent(ev);

return gesturedetector.onTouchEvent(ev);

}

class MyGestureListener 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;

@Override

public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,

float velocityY) {

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

float dY = e1.getY()-e2.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(getApplicationContext(), “Right Swipe”, Toast.LENGTH_SHORT).show();

} else {

Toast.makeText(getApplicationContext(), “Left Swipe”, Toast.LENGTH_SHORT).show();

}

return true;

} else if (Math.abs(dX)<SWIPE_MAX_OFF_PATH &&

Math.abs(velocityY)>=SWIPE_THRESHOLD_VELOCITY &&

Math.abs(dY)>=SWIPE_MIN_DISTANCE ) {

if (dY>0) {

Toast.makeText(getApplicationContext(), “Up Swipe”, Toast.LENGTH_SHORT).show();

} else {

Toast.makeText(getApplicationContext(), “Down Swipe”, Toast.LENGTH_SHORT).show();

}

return true;

}

return false;

}

}

}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top