我正在考虑创建一个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;
}
}
以下是给出错误的新代码
对于类型Object
,未定义onTouch(View,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 :(得分:4)
出于某种原因,我认为你有一个运行时错误,但是没有,你甚至在尝试编译之前就在Eclipse中看到了一条错误消息。即使编译了该代码,您也必须对布局容器实际应用手势检测器,以便它可以执行任何操作。
您在super.onTouch()
类中调用OnSwipeTouchListener
,这是Object的子类。对象没有onTouch()
方法,这就是编译器所抱怨的。 onTouch()
方法需要进入您的活动,而不是自定义手势监听器。
我猜你无论用什么样的例子,你都会对所有切割和粘贴感到困惑。
这是一些非常简单的示例代码,用于检测演示如何应用自定义手势监听器的定向flings:
http://pcfandroid.wordpress.com/2011/07/17/swipe-with-android-android-tutorial/
这篇文章易于理解,非常清晰,非常简单。
答案 1 :(得分: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;
}
}
}