2012-09-23 90 views
0

我一直在努力讓自己的觀點對用戶滑動做出響應。我在主要活動中運行良好,並且我希望在多個活動中重複使用此代碼,而無需每次都複製和粘貼它。如何在Android中重用監聽器

我想找到一種方法將此偵聽器移至單獨的類(和文件),然後調用可能在每個活動中唯一定義的不同響應。我能夠想到做到這一點的唯一方法是將Activity擴展爲自定義類(XActivity)。但是這樣做只會在我使用自定義類XActivity而不是ListActivity時起作用。

有沒有人知道一種方法來隔離和使用Listener作爲自定義類,以便我不必複製它一遍又一遍地?

View_Main package us.cimple.testswipe;

import android.app.Activity; 
import android.content.Context; 
import android.content.Intent; 
import android.os.Bundle; 
import android.util.DisplayMetrics; 
import android.util.Log; 
import android.view.GestureDetector; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.GestureDetector.SimpleOnGestureListener; 
import android.view.ViewConfiguration; 
import android.widget.Toast; 

public class View_Main extends Activity { 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    // Implement Gesture detection 
    gestureDetector = new GestureDetector(new MyGestureDetector()); 
    gestureListener = new View.OnTouchListener() { 
     public boolean onTouch(View v, MotionEvent event) { 
      return gestureDetector.onTouchEvent(event); 
     } 
    }; 
    View v = (View) getWindow().getDecorView().getRootView(); 
    v.setOnTouchListener(gestureListener); 
} 

private void openSecond(){ 
    Intent i = new Intent(this, View_Second.class); 
    startActivity(i); 
} 

public final void toast(String message) { 
    Toast.makeText(getBaseContext(), message, Toast.LENGTH_SHORT).show(); 
} 


/** 
* Gestures 
*/ 
private GestureDetector gestureDetector; 
View.OnTouchListener gestureListener; 

private class MyGestureDetector extends SimpleOnGestureListener { 
    final ViewConfiguration vc = ViewConfiguration.get(getBaseContext()); 
    final int swipeMinDistance = vc.getScaledTouchSlop() * 2; 
    final int swipeThresholdVelocity = vc.getScaledMinimumFlingVelocity(); 

    @Override 
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { 
     try { 
      /** 
      if (Math.abs(e1.getY() - e2.getY()) > swipeMinDistance){ 
       return false; 
      } 
      */ 

      float deltaX = 0; 
      float deltaY = 0; 

      deltaX = e1.getX() - e2.getX(); 
      deltaX = Math.abs(deltaX); 

      deltaY = e1.getY() - e2.getY(); 
      deltaY = Math.abs(deltaY); 

      Log.i("View_Main - deltaX", Float.toString(deltaX)); 
      Log.i("View_Main - deltaY", Float.toString(deltaY)); 

      Log.i("View_Main - swipe e1", Float.toString(e1.getX()) +" , " + Float.toString(e1.getY())); 
      Log.i("View_Main - swipe e2", Float.toString(e2.getX()) +" , " + Float.toString(e2.getY())); 

      //Vertical Swipe 
      if (deltaY > deltaX){ 
       if(e1.getY() - e2.getY() > swipeMinDistance*2 && Math.abs(velocityY) > swipeThresholdVelocity) { 
       onBottomToTopSwipe(); 
      } else if (e2.getY() - e1.getY() > swipeMinDistance *2 && Math.abs(velocityY) > swipeThresholdVelocity) { 
       onTopToBottomSwipe(); 
       } 
      } 

      //Horizontal Swipe 
      else { 
       if(e1.getX() - e2.getX() > swipeMinDistance && Math.abs(velocityX) > swipeThresholdVelocity) { 
        onRightToLeftSwipe(); 
       } else if (e2.getX() - e1.getX() > swipeMinDistance && Math.abs(velocityX) > swipeThresholdVelocity) { 
        onLeftToRightSwipe(); 
       } 
      }  


     } catch (Exception e) { 
      // nothing 
     } 
     return false; 
    } 
} 


private void onRightToLeftSwipe() { 
    //do something  
    toast("Right to Left"); 
    openSecond(); 
} 

private void onLeftToRightSwipe() { 
    toast("Left to Right"); 
    onBackPressed(); 
} 

private void onBottomToTopSwipe() { 
    toast("Bottom to Top"); 
    //do something 
} 

private void onTopToBottomSwipe(){ 
    toast("Top to Bottom"); 
    //do something 
} 
} 

感謝

回答

0

通過活動更換ListActivity。你將需要在這個活動裏面使用一個listview。 現在繼續你提到的方式。即全部由一些XActivity類擴展。

但是,您可以嘗試更多的修補程序,如在BaseListActivity和BaseActivity上創建,並在其中使用此偵聽器,而不一定需要重複代碼,可以像植入單獨的類一樣。