Android 安卓右击有问题

Android 安卓右击有问题,android,swipe,Android,Swipe,我在文本视图中显示的字符串数组项目中有一些动物名称。我想要的是,当我向左滑动时,字符串数组项目中的下一个动物名称将显示在文本视图中,当我向右滑动时,将显示上一个名称。我可以使用计数器管理左击事件,但不知道如何管理右击 OnSwipeTouchListener.java public class OnSwipeTouchListener implements OnTouchListener { protected final GestureDetector gestureDetector

我在文本视图中显示的字符串数组项目中有一些动物名称。我想要的是,当我向左滑动时,字符串数组项目中的下一个动物名称将显示在文本视图中,当我向右滑动时,将显示上一个名称。我可以使用计数器管理左击事件,但不知道如何管理右击

OnSwipeTouchListener.java

public class OnSwipeTouchListener implements OnTouchListener {

    protected final GestureDetector gestureDetector;
    public OnSwipeTouchListener (Context ctx){
          gestureDetector = new GestureDetector(ctx, new GestureListener());
    }

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

    @Override
    public boolean onTouch(View arg0, MotionEvent arg1) {
        // TODO Auto-generated method stub
        return false;
    }
}
javas.java

public class animals extends Activity {
    TextView tv;
    int counter = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.animals);

        tv= (TextView) findViewById(R.id.tv_animals);
        tv.setOnTouchListener(new OnSwipeTouchListener(null) {
            public void onSwipeTop() {

                Toast.makeText(getApplicationContext(), "top", Toast.LENGTH_SHORT).show();
            }

            public void onSwipeRight() {
// how to swipe right? Like if 4th animal name is getting displayed and i swipe right , then the 3rd name should be visible 
            }

            public void onSwipeLeft() {

                String[] mer = getResources().getStringArray(
                        R.array.animals);

                tv.setText(mer[counter]);
                counter++;

            }

            public void onSwipeBottom() {

                Toast.makeText(getApplicationContext(), "bottom", 1000).show();
            }

            public boolean onTouch(View v, MotionEvent event) {
                return gestureDetector.onTouchEvent(event);

            }
        });
    }
}
使用此代码:

public class MainActivity extends Activity {

TextView gestureEvent;
ArrayList<String> ana=new ArrayList<String>();
public static int k=0;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
   for(int i=0;i<10;i++)
       ana.add(String.valueOf(i));

   gestureEvent = (TextView)findViewById(R.id.gesture);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
return gestureDetector.onTouchEvent(event);
}

 SimpleOnGestureListener simpleOnGestureListener
    = new SimpleOnGestureListener(){


 @Override
  public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
    float velocityY) {
   String swipe = "";
    float sensitvity = 50;

     // TODO Auto-generated method stub
    if((e1.getX() - e2.getX()) > sensitvity){
     swipe += ana.get(k++);
   }else if((e2.getX() - e1.getX()) > sensitvity){
      swipe += ana.get(k--);
     }else{
    swipe += "\n";
      }

  if((e1.getY() - e2.getY()) > sensitvity){
   swipe += ana.get(k++);
  }else if((e2.getY() - e1.getY()) > sensitvity){
   swipe += ana.get(k--);
  }else{
    swipe += "\n";
   }

   gestureEvent.setText(swipe);

   return super.onFling(e1, e2, velocityX, velocityY);
  }
     };

         GestureDetector gestureDetector
         = new GestureDetector(simpleOnGestureListener);
    }
公共类MainActivity扩展活动{
TextView手势事件;
ArrayList ana=新的ArrayList();
公共静态int k=0;
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
for(int i=0;i灵敏度){
swipe+=ana.get(k++);
}else if((e2.getX()-e1.getX())>sensitivity){
swipe+=ana.get(k--);
}否则{
滑动+=“\n”;
}
if((e1.getY()-e2.getY())>sensitivity){
swipe+=ana.get(k++);
}else if((e2.getY()-e1.getY())>sensitivity){
swipe+=ana.get(k--);
}否则{
滑动+=“\n”;
}
gestureEvent.setText(滑动);
返回super.onFling(e1、e2、velocityX、velocityY);
}
};
手势检测器手势检测器
=新的手势检测器(simpleOnGestureListener);
}