Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package info.androidhive.slidingmenu;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.List;
- import android.app.Activity;
- import android.util.Log;
- import android.view.MotionEvent;
- import android.view.View;
- import android.widget.TextView;
- import android.widget.Toast;
- public class ActivitySwipeDetector implements View.OnTouchListener {
- static final String logTag = "ActivitySwipeDetector";
- private Activity activity;
- static final int MIN_DISTANCE = 100;
- private float downX, downY, upX, upY;
- private TextView txt;
- int textviewindex=1;
- String stanza;
- String[] strArrays;
- List<String> strArray;
- public ActivitySwipeDetector(Activity activity){
- this.activity = activity;
- txt = (TextView) ((Activity)activity).findViewById(R.id.songtext);
- stanza=txt.getText().toString();
- strArrays = stanza.split("\n\n\n");
- strArray = new ArrayList<String>(Arrays.asList(strArrays));
- }
- public void onRightToLeftSwipe(){
- Log.i(logTag, "RightToLeftSwipe!");
- // activity.doSomething();
- }
- public void onLeftToRightSwipe(){
- Log.i(logTag, "LeftToRightSwipe!");
- // activity.doSomething();
- }
- public void onTopToBottomSwipe(){
- Log.i(logTag, "onTopToBottomSwipe!");
- // activity.doSomething();
- }
- public void onBottomToTopSwipe(){
- Log.i(logTag, "onBottomToTopSwipe!");
- // activity.doSomething();
- }
- public boolean onTouch(View v, MotionEvent event) {
- switch(event.getAction()){
- case MotionEvent.ACTION_DOWN: {
- downX = event.getX();
- downY = event.getY();
- return true;
- }
- case MotionEvent.ACTION_UP: {
- upX = event.getX();
- upY = event.getY();
- float deltaX = downX - upX;
- float deltaY = downY - upY;
- // swipe horizontal?
- if(Math.abs(deltaX) > Math.abs(deltaY))
- {
- if(Math.abs(deltaX) > MIN_DISTANCE){
- // left or right
- if(deltaX < 0) { //this.onRightSwipe(); return true; }
- if(deltaX > 0) { //this.onLeftSwipe(); return true; }
- }
- else {
- Log.i(logTag, "Horizontal Swipe was only " + Math.abs(deltaX) + " long, need at least " + MIN_DISTANCE);
- return false; // We don't consume the event
- }
- }
- // swipe vertical?
- else
- {
- if(Math.abs(deltaY) > MIN_DISTANCE){
- // top or down
- if(deltaY < 0) { this.onDownSwipe(); return true; }
- if(deltaY > 0) { this.onUpSwipe(); return true; }
- }
- else {
- Log.i(logTag, "Vertical Swipe was only " + Math.abs(deltaX) + " long, need at least " + MIN_DISTANCE);
- return false; // We don't consume the event
- }
- }
- return true;
- }
- }
- if (downX < upY)
- {
- // Toast.makeText(ActivitySwipeDetector.this, "Up to down",Toast.LENGTH_SHORT).show();
- onDownSwipe();
- Log.d("swipe","up to down");
- return true;
- }
- //if Down to UP sweep event on screen
- if (downY > upY)
- {
- onUpSwipe();
- Log.d("swipe","down to up");
- return true;
- // Toast.makeText(this, "Down to UP Swap Performed", Toast.LENGTH_LONG).show();
- }
- // break;
- return true;
- }
- }
- return false;
- }
- private void onDownSwipe() {
- // TODO Auto-generated method stub
- if(textviewindex<strArray.size() && textviewindex!=0)
- {
- String singlestanza=strArray.get(textviewindex);
- System.out.println(singlestanza);
- Log.d("swipe", "down swipe!");
- txt.setText(singlestanza);
- textviewindex++;
- }
- }
- private void onUpSwipe() {
- // TODO Auto-generated method stub
- Log.d("swipe", "up swipe!");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement