Advertisement
asifa

Untitled

Jul 25th, 2014
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.91 KB | None | 0 0
  1. package info.androidhive.slidingmenu;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Arrays;
  5. import java.util.List;
  6.  
  7. import android.app.Activity;
  8. import android.util.Log;
  9. import android.view.MotionEvent;
  10. import android.view.View;
  11. import android.widget.TextView;
  12. import android.widget.Toast;
  13.  
  14. public class ActivitySwipeDetector implements View.OnTouchListener {
  15.  
  16. static final String logTag = "ActivitySwipeDetector";
  17. private Activity activity;
  18. static final int MIN_DISTANCE = 100;
  19. private float downX, downY, upX, upY;
  20. private TextView txt;
  21. int textviewindex=1;
  22. String stanza;
  23. String[] strArrays;
  24. List<String> strArray;
  25.  
  26. public ActivitySwipeDetector(Activity activity){
  27.     this.activity = activity;
  28.     txt = (TextView) ((Activity)activity).findViewById(R.id.songtext);  
  29.    
  30.    
  31.     stanza=txt.getText().toString();
  32.     strArrays = stanza.split("\n\n\n");
  33.     strArray = new ArrayList<String>(Arrays.asList(strArrays));
  34.    
  35.  }
  36.  
  37.  
  38. public void onRightToLeftSwipe(){
  39.     Log.i(logTag, "RightToLeftSwipe!");
  40.  //   activity.doSomething();
  41. }
  42.  
  43. public void onLeftToRightSwipe(){
  44.     Log.i(logTag, "LeftToRightSwipe!");
  45.    // activity.doSomething();
  46. }
  47.  
  48. public void onTopToBottomSwipe(){
  49.     Log.i(logTag, "onTopToBottomSwipe!");
  50.    // activity.doSomething();
  51. }
  52.  
  53. public void onBottomToTopSwipe(){
  54.     Log.i(logTag, "onBottomToTopSwipe!");
  55. //    activity.doSomething();
  56. }
  57.  
  58. public boolean onTouch(View v, MotionEvent event) {
  59.     switch(event.getAction()){
  60.         case MotionEvent.ACTION_DOWN: {
  61.             downX = event.getX();
  62.             downY = event.getY();
  63.             return true;
  64.         }
  65.         case MotionEvent.ACTION_UP: {
  66.             upX = event.getX();
  67.             upY = event.getY();
  68.  
  69.             float deltaX = downX - upX;
  70.             float deltaY = downY - upY;
  71.        
  72.        // swipe horizontal?
  73.         if(Math.abs(deltaX) > Math.abs(deltaY))
  74.         {
  75.             if(Math.abs(deltaX) > MIN_DISTANCE){
  76.                 // left or right
  77.                 if(deltaX < 0) { //this.onRightSwipe(); return true; }
  78.                 if(deltaX > 0) { //this.onLeftSwipe(); return true; }
  79.             }
  80.             else {
  81.                     Log.i(logTag, "Horizontal Swipe was only " + Math.abs(deltaX) + " long, need at least " + MIN_DISTANCE);
  82.                     return false; // We don't consume the event
  83.             }
  84.         }
  85.         // swipe vertical?
  86.         else
  87.         {
  88.             if(Math.abs(deltaY) > MIN_DISTANCE){
  89.                 // top or down
  90.                 if(deltaY < 0) { this.onDownSwipe(); return true; }
  91.                 if(deltaY > 0) { this.onUpSwipe(); return true; }
  92.             }
  93.             else {
  94.                     Log.i(logTag, "Vertical Swipe was only " + Math.abs(deltaX) + " long, need at least " + MIN_DISTANCE);
  95.                     return false; // We don't consume the event
  96.             }
  97.         }
  98.  
  99.             return true;
  100.         }
  101.     }
  102.         if (downX < upY)
  103.         {
  104.            // Toast.makeText(ActivitySwipeDetector.this, "Up to down",Toast.LENGTH_SHORT).show();
  105.         onDownSwipe();
  106.             Log.d("swipe","up to down");
  107.   return true;
  108.         }
  109.        
  110.         //if Down to UP sweep event on screen
  111.         if (downY > upY)
  112.         {
  113.             onUpSwipe();
  114.             Log.d("swipe","down to up");
  115.     return true;        
  116.             //    Toast.makeText(this, "Down to UP Swap Performed", Toast.LENGTH_LONG).show();
  117.          }
  118.       //  break;
  119.  
  120.     return true;
  121.         }
  122.      }
  123.     return false;
  124. }
  125.  
  126. private void onDownSwipe() {
  127.     // TODO Auto-generated method stub
  128.    
  129.     if(textviewindex<strArray.size() && textviewindex!=0)
  130.     {
  131.    
  132.     String singlestanza=strArray.get(textviewindex);
  133.    
  134.     System.out.println(singlestanza);
  135.    
  136.    
  137.     Log.d("swipe", "down swipe!");
  138.     txt.setText(singlestanza);
  139.     textviewindex++;
  140.     }
  141. }
  142. private void onUpSwipe() {
  143.     // TODO Auto-generated method stub
  144.     Log.d("swipe", "up swipe!");
  145.    
  146. }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement