Advertisement
asifa

Untitled

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