2012-06-23 3 views
0

이것은 OnListGestureDetectorTest 인터페이스 (아래의 두 번째 샘플 코드)를 구현하는 setOnListGestureDetectorTest 메소드를 호출하여 ListGestureDetector 클래스의 메소드를 오버라이드하려고하는 액티비티 클래스의 샘플 코드이다. 철저한 디버거로 넘어 가기 onRTLFling, onLTRFling 및 customOnItemClick을 오버라이드 한 함수가 결코 호출되지 않는다는 것을 알고 있습니다. ListGestureDetector 클래스에서 원래의 빈 함수 만이 호출됩니다. 내가 뭘 잘못하고 왜 overriden 함수를 호출하지?안드로이드가 오버라이드 한 메소드는 결코 호출하지 않는다

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); 

그리고 이것은 내가 찾은

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

ListGestureDetector 클래스입니다 제대로 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; 
} 

} 
관련 문제