我正在考虑创建一个 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() {
}
}