我想只在用户滚动到某个位置时调用一个函数。目前我正在做以下事情:
view.addOnScrollChangeListener(){
int[] locA = v1.getLocationOnScreen(); // it gives x & y location
int[] locB = v2.getLocationOnScreen(); // it gives x & y location
if(locA[1] == threshold) callFunctionA();
if(locB[1] == threshold) callFunctionB();
}
问题是,如果用户滚动太快,它永远不会调用callFunctionA() or callFunctionB
。怎么解决这个问题?
答案 0 :(得分:0)
这应该有效。如果您滚动经过某个点,则会触发此操作,如果再次在阈值之前滚动,则会再次发生。
boolean funcACalled = false;
boolean funcBCalled = false;
view.addOnScrollChangeListener(){
int[] locA = v1.getLocationOnScreen(); // it gives x & y location
int[] locB = v2.getLocationOnScreen(); // it gives x & y location
if(locA[1] > threshold && !funcACalled){
callFunctionA();
funcACalled = true;
}
if(locB[1] > threshold && !funcBCalled){
callFunctionB();
funcBCalled = true;
}
if(locA[1] < threshold) funcACalled = false;
if(locB[1] < threshold) funcBCalled = false;
}