这是来自 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 类
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) {
}
}