Android – ListView重复事件
作者:互联网
在我的应用程序中,我有一个由ArrayAdapter支持的ListView.在其中我正在检测OnScrollListener#onScroll方法中的事件以查找列表的结尾.我注意到在手机(MyTouch)上,跟踪球和手势/触摸滚动都会触发事件两次.在模拟器上,我通过滚轮和点击药物滚动获得相同的行为.但是在模拟器中,如果我使用向下箭头按钮滚动事件只触发一次.
这是代码:
this.view.setOnScrollListener(new OnScrollListener() {
@Override
public void onScroll(final AbsListView view, final int first,
final int visible, final int total) {
// detect if last item is visible
if (visible < total && (first + visible == total)) {
Log.d("OnScrollListener - end of list", "fvi: " +
first + ", vic: " + visible + ", tic: " + total);
// this line gets called twice
onLastListItemDisplayed(total, visible);
}
}
}
如何抑制或处理此行为?我只需要一个事件,并且尽量不要恢复到像布尔字段那样的愚蠢的黑客攻击.
据我所知 – 两个事件都有相同的堆栈跟踪
Thread [<3> main] (Suspended (breakpoint at line 116 in SearchResultsView$4))
SearchResultsView$4.onScroll(AbsListView, int, int, int) line: 116
ListView(AbsListView).invokeOnItemScrollListener() line: 655
ListView.arrowScrollImpl(int) line: 2256
ListView.arrowScroll(int) line: 2172
ListView.commonKey(int, int, KeyEvent) line: 1977
ListView.onKeyMultiple(int, int, KeyEvent) line: 1929
KeyEvent.dispatch(KeyEvent$Callback) line: 899
ListView(View).dispatchKeyEvent(KeyEvent) line: 3647
ListView(ViewGroup).dispatchKeyEvent(KeyEvent) line: 744
ListView.dispatchKeyEvent(KeyEvent) line: 1909
FrameLayout(ViewGroup).dispatchKeyEvent(KeyEvent) line: 746
LinearLayout(ViewGroup).dispatchKeyEvent(KeyEvent) line: 746
PhoneWindow$DecorView(ViewGroup).dispatchKeyEvent(KeyEvent) line: 746
PhoneWindow$DecorView.superDispatchKeyEvent(KeyEvent) line: 1708
PhoneWindow.superDispatchKeyEvent(KeyEvent) line: 1197
SearchResultsView(Activity).dispatchKeyEvent(KeyEvent) line: 1967
PhoneWindow$DecorView.dispatchKeyEvent(KeyEvent) line: 1684
ViewRoot.deliverKeyEventToViewHierarchy(KeyEvent, boolean) line: 2329
ViewRoot.handleFinishedEvent(int, boolean) line: 2299
ViewRoot.handleMessage(Message) line: 1621
ViewRoot(Handler).dispatchMessage(Message) line: 99
Looper.loop() line: 123
ActivityThread.main(String[]) line: 4203
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]
Method.invoke(Object, Object...) line: 521
ZygoteInit$MethodAndArgsCaller.run() line: 791
ZygoteInit.main(String[]) line: 549
NativeStart.main(String[]) line: not available [native method]
P.P.S.并通过非常有用的回复关闭轨迹球和触摸屏非常快速地发送几个运动事件.此行为是预期的.
解决方法:
由于谷歌的优秀人士说这就像预期的那样,处理这个对我有用的问题是一种丑陋的野蛮方式.基本上这里的假设是,在发布事件时,我将上次保存的第一项索引与onScroll调用中传递的索引进行比较.我只会处理事件,如果这些不匹配:
this.view.setOnScrollListener(new OnScrollListener() {
private int lastSavedFirst = -1;
@Override
public void onScroll(final AbsListView view, final int first, final int visible, final int total) {
// detect if last item is visible
if (visible < total && (first + visible == total)) {
// only process first event
if (first != lastSavedFirst) {
lastSavedFirst = first;
Log.d("OnScrollListener - end of list", "fvi: " + first + ", vic: " + visible + ", tic: "
+ total);
onLastListItemDisplayed(total, visible);
}
}
}
@Override
public void onScrollStateChanged(final AbsListView view, final int scrollState) {
// Log.d("OnScrollListener", "state: " + scrollState);
}
});
标签:android,listview,onscroll 来源: https://codeday.me/bug/20190627/1301451.html