永远不会调用Android覆盖方法

时间:2012-06-23 08:46:46

标签: android override

这是来自Activity类的示例代码,我尝试通过调用实现OnListGestureDetectorTest接口的setOnListGestureDetectorTest方法来覆盖ListGestureDetector类中的方法(下面的第二个示例代码)。进行彻底的调试我意识到onRTLFling,onLTRFling和customOnItemClick上的覆盖函数从未被调用过。只有ListGestureDetector类中有原始的空函数。我做错了什么以及为什么覆盖函数从不调用?

    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 Class

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) {       
}

}

1 个答案:

答案 0 :(得分: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;
}

}