2013-04-22 86 views
2

我正在考虑创建一个Android应用程序作为更广泛的艺术项目的一部分,并具有Android开发没有经验需要检查,如果事情是可能/可行的。左/右和上/下刷卡导航

它需要一个非常简单的接口,数个(15 - 20)之间导航包含图像或文本页面。导航需要左/右和向上/向下滑动。

我已经(从以往关于这一主题的问题通过复制/粘贴代码)在一个非常简单的应用程序使用PagerAdapter/OnClickListener实现概念的证据,但这不仅左/右切换。

是否有使用此页面之间向上/向下导航的方法吗?任何帮助/建议将不胜感激,

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; 
} 
} 

下面是新的代码给错误

的方法onTouch(查看,MotionEvent)是未定义的类型对象

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() { 
    } 
} 
+0

请选中该http://stackoverflow.com/questions/4139288/android-how-to-handle-right-to-left-swipe-gestures和http://stackoverflow.com/questions/12970249/Android的刷卡左,或右到幻灯片视图 – 2013-04-22 11:23:45

+0

请选中该http://stackoverflow.com/questions/937313/android-basic-gesture-detection和 http://stackoverflow.com/问题/ 12970249/android-swipe-left-right-to-slide-views – 2013-04-22 11:33:09

+0

@ pragnesh-soni谢谢你,关注http://stackoverflow.com/questions/4139288/android-how-to-handle-right-左手轻扫手势,我留下了错误“方法onTouch(视图,MotionEvent)未定义的类型对象”,在super.onTouch(视图,motionEvent)行; ---我无法删除这个, – dantibb 2013-04-22 16:17:47

回答

4

出于某种原因,我认为你有一个运行时错误,但不是,在你尝试编译之前,你很明显在Eclipse中看到一条错误消息。即使编译了代码,您也必须将您的手势检测器应用于布局容器,以便它能够执行任何操作。

您在您的OnSwipeTouchListener类中称为super.onTouch(),它是Object的一个子类。 Object没有onTouch()方法,这就是编译器所抱怨的。 onTouch()方法需要在您的活动中进行,而不是在您的自定义手势监听器中。

我要去猜你使用,无论例如,你得到了所有的剪切和粘贴有点混乱。

这里的检测方向甩,演示了如何应用自定义手势听者一些非常简单的示例代码:

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

这篇文章是死的容易执行,很清晰,很简单的。

+0

谢谢你,是的,这个链接让我更清楚地了解手势检测,并让它工作。虽然不是我之后所做的,所以要试用垂直ViewPager https://github.com/JakeWharton/Android-DirectionalViewPager/ - 再次感谢。 – dantibb 2013-04-25 16:46:00

2
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; 

} 

} 

} 
相关问题