2012-06-23 67 views
0

這是Activity類的示例代碼,我嘗試通過調用實現OnListGestureDetectorTest接口的setOnListGestureDetectorTest方法(下面的第二個示例代碼)覆蓋ListGestureDetector類的方法。進行徹底的調試,我意識到覆蓋函數onRTLFling,onLTRFling和customOnItemClick從來沒有調用過。只有從ListGestureDetector類中調用原始的空函數。我做錯了什麼,以及爲什麼overriden函數永遠不會調用?Android overrided methods never calls

ListGestureDetector listGestureDetectorListener = new ListGestureDetector(
      this, mEarningsListView); 

    listGestureDetectorListener 
      .setOnListGestureDetectorTest(new ListGestureDetector.OnListGestureDetectorTest() { 

       @Override 
       public void onRTLFling(int pos) { 
        Log.d(TAG,"onRTLFling"); 

       } 

       @Override 
       public void onLTRFling(int pos) { 
        Log.d(TAG,"onLTRFling"); 

       } 

       @Override 
       public void customOnItemClick(int position) { 
        Log.d(TAG,"onLTRFling"); 
       } 
      }); 

    final GestureDetector gestureDetector = new GestureDetector(
      listGestureDetectorListener); 
    View.OnTouchListener gestureListener = new View.OnTouchListener() { 
     public boolean onTouch(View v, MotionEvent event) { 
      return gestureDetector.onTouchEvent(event); 
     } 
    }; 

    mEarningsListView.setOnTouchListener(gestureListener); 

這是ListGestureDetector類

public class ListGestureDetector extends SimpleOnGestureListener { 

private int REL_SWIPE_MIN_DISTANCE; 
private int REL_SWIPE_MAX_OFF_PATH; 
private int REL_SWIPE_THRESHOLD_VELOCITY; 
private ListView mList; 

// Detect a single-click and call my own handler. 
@Override 
public boolean onSingleTapUp(MotionEvent e) { 
    int pos = mList.pointToPosition((int) e.getX(), (int) e.getY()); 
    customOnItemClick(pos); 
    return false; 
} 

@Override 
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, 
     float velocityY) { 
    if (Math.abs(e1.getY() - e2.getY()) > REL_SWIPE_MAX_OFF_PATH) 
     return false; 
    if (e1.getX() - e2.getX() > REL_SWIPE_MIN_DISTANCE 
      && Math.abs(velocityX) > REL_SWIPE_THRESHOLD_VELOCITY) { 
     int pos = mList.pointToPosition((int) e1.getX(), 
       (int) e1.getY()); 
     onRTLFling(pos); 
    } else if (e2.getX() - e1.getX() > REL_SWIPE_MIN_DISTANCE 
      && Math.abs(velocityX) > REL_SWIPE_THRESHOLD_VELOCITY) { 
     int pos = mList.pointToPosition((int) e1.getX(), 
       (int) e1.getY()); 
     onLTRFling(pos); 
    } 
    return false; 
} 

public ListGestureDetector(Context c, ListView list) { 
    super(); 
    mList = list; 
    // Density-aware measurements 
    DisplayMetrics dm = c.getResources().getDisplayMetrics(); 
    REL_SWIPE_MIN_DISTANCE = (int) (120.0f * dm.densityDpi/160.0f + 0.5); 
    REL_SWIPE_MAX_OFF_PATH = (int) (250.0f * dm.densityDpi/160.0f + 0.5); 
    REL_SWIPE_THRESHOLD_VELOCITY = (int) (200.0f * dm.densityDpi/160.0f + 0.5); 
} 


interface OnListGestureDetectorTest { 
    void customOnItemClick(int position); 
    void onRTLFling(int pos); 
    void onLTRFling(int pos); 
} 

public void customOnItemClick(int position) { 
} 

public void onRTLFling(int pos) {  
} 

public void onLTRFling(int pos) {  
} 

public void setOnListGestureDetectorTest(OnListGestureDetectorTest ogd) {  
} 

} 

回答

0

我發現解決方案如何http://tseng-blog.nge-web.net/blog/2009/02/17/how-implement-your-own-listener-android-java/

我改變根據上面的鏈接ListGestureDetector類的鏈接正確地實現監聽器:

public class ListGestureDetector extends SimpleOnGestureListener { 

private int REL_SWIPE_MIN_DISTANCE; 
private int REL_SWIPE_MAX_OFF_PATH; 
private int REL_SWIPE_THRESHOLD_VELOCITY; 
private ListView mList; 

OnListGestureDetector onListGestureDetector = null; 

// Detect a single-click and call my own handler. 
@Override 
public boolean onSingleTapUp(MotionEvent e) { 
    int pos = mList.pointToPosition((int) e.getX(), (int) e.getY()); 
    onListGestureDetector.customOnItemClick(pos); 
    return false; 
} 

@Override 
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, 
     float velocityY) { 
    if (Math.abs(e1.getY() - e2.getY()) > REL_SWIPE_MAX_OFF_PATH) 
     return false; 
    if (e1.getX() - e2.getX() > REL_SWIPE_MIN_DISTANCE 
      && Math.abs(velocityX) > REL_SWIPE_THRESHOLD_VELOCITY) { 
     int pos = mList.pointToPosition((int) e1.getX(), 
       (int) e1.getY()); 
     onListGestureDetector.onRTLFling(pos); 
    } else if (e2.getX() - e1.getX() > REL_SWIPE_MIN_DISTANCE 
      && Math.abs(velocityX) > REL_SWIPE_THRESHOLD_VELOCITY) { 
     int pos = mList.pointToPosition((int) e1.getX(), 
       (int) e1.getY()); 
     onListGestureDetector.onLTRFling(pos); 
    } 
    return false; 
} 

public ListGestureDetector(Context c, ListView list) { 
    super(); 
    mList = list; 
    // Density-aware measurements 
    DisplayMetrics dm = c.getResources().getDisplayMetrics(); 
    REL_SWIPE_MIN_DISTANCE = (int) (120.0f * dm.densityDpi/160.0f + 0.5); 
    REL_SWIPE_MAX_OFF_PATH = (int) (250.0f * dm.densityDpi/160.0f + 0.5); 
    REL_SWIPE_THRESHOLD_VELOCITY = (int) (200.0f * dm.densityDpi/160.0f + 0.5); 
} 


interface OnListGestureDetector { 
    public abstract void customOnItemClick(int position); 
    public abstract void onRTLFling(int pos); 
    public abstract void onLTRFling(int pos); 
} 

public void setOnListGestureDetector(OnListGestureDetector ogd) { 
    onListGestureDetector = ogd; 
} 

}