Android overrided methods never calls
这是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) {
}
}
回答:
我发现解决方案如何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;
}
}
以上是 Android overrided methods never calls 的全部内容, 来源链接: utcz.com/qa/265302.html