2014-03-26 32 views
0

我有一些動物名稱在字符串數組項目,我顯示在文本View.What我想要的是,當我向左滑動,字符串數組項目中的下一個動物名稱將顯示到文本視圖,當我向右滑動時,先前的名稱將顯示。我能夠使用計數器管理左側滑動事件,但不知道如何管理右側滑動。正確的滑動Android的問題

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

animals.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); 

      } 
     }); 
    } 
} 

回答

0

使用此代碼:

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