Android:ScrollView 'setOnScrollListener'(如 ListView)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3513594/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android: ScrollView 'setOnScrollListener' (like ListView)
提问by Erik
I want to do something when the user has scrolled >90% down, so I thought I could just add a onScrollListener like I would in a ListView. Unfortunatly, ScrollView doesn't seem to have a similar method. Is there any way I can do what I want; getting a notification when the user scrolls approx 90% down?
我想在用户向下滚动 > 90% 时做一些事情,所以我想我可以像在 ListView 中一样添加一个 onScrollListener 。不幸的是, ScrollView 似乎没有类似的方法。有什么办法可以做我想做的事吗?当用户向下滚动大约 90% 时收到通知?
Thanks, Erik
谢谢,埃里克
采纳答案by Rich Schuler
You can try extending ScrollView
and overriding View#onScrollChanged
and the doing your checks there. You have to extend ScrollView
since onScrollChanged
is a protected method.
您可以尝试扩展ScrollView
和覆盖View#onScrollChanged
并在那里进行检查。您必须扩展,ScrollView
因为onScrollChanged
它是受保护的方法。
回答by mmathieum
This is what I end up doing to know if the user was interacting with my ScrollView
or not:
这就是我最终要知道用户是否与我的交互ScrollView
:
findViewById(R.id.scrollview).setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_SCROLL:
case MotionEvent.ACTION_MOVE:
setScrollState(OnScrollListener.SCROLL_STATE_FLING);
break;
case MotionEvent.ACTION_DOWN:
setScrollState(OnScrollListener.SCROLL_STATE_TOUCH_SCROLL);
break;
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
setScrollState(OnScrollListener.SCROLL_STATE_IDLE);
break;
}
return false;
}
});
回答by Frederick Nyawaya
You can implement an onTouchListener and use the getScrollX and getScrollY callbacks of the view
您可以实现 onTouchListener 并使用视图的 getScrollX 和 getScrollY 回调
回答by android developer
If you want a scrolling view that works like a listview, may I suggest trying out a horizontal listView library:
如果你想要一个像列表视图一样工作的滚动视图,我可以建议尝试一个水平的 listView 库:
https://github.com/sephiroth74/HorizontalVariableListView
https://github.com/sephiroth74/HorizontalVariableListView
it's based on the original ListView, and it works very well.
它基于原始的 ListView,并且运行良好。
it has its OnScrollListener , and even though the website says it supports Android 2.3 and above, I think it should work from API 7 and above (and with some tweaks even older).
它有它的 OnScrollListener ,尽管该网站说它支持 Android 2.3 及更高版本,但我认为它应该可以从 API 7 及更高版本运行(并且有一些更旧的调整)。