简体   繁体   English

Android:ScrollView'setOnScrollListener'(如ListView)

[英]Android: ScrollView 'setOnScrollListener' (like ListView)

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. 当用户向下滚动> 90%时,我想做一些事情,所以我想我可以像在ListView中一样添加onScrollListener。 Unfortunatly, ScrollView doesn't seem to have a similar method. 不幸的是,ScrollView似乎没有类似的方法。 Is there any way I can do what I want; 有什么方法可以做我想做的事; getting a notification when the user scrolls approx 90% down? 当用户向下滚动约90%时收到通知?

Thanks, Erik 谢谢,Erik

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;
    }
});

You can try extending ScrollView and overriding View#onScrollChanged and the doing your checks there. 您可以尝试扩展ScrollView并覆盖View#onScrollChanged并在那里进行检查。 You have to extend ScrollView since onScrollChanged is a protected method. 您必须扩展ScrollView因为onScrollChanged是受保护的方法。

您可以实现onTouchListener并使用视图的getScrollX和getScrollY回调

您可以使用此链接获取所需内容。

If you want a scrolling view that works like a listview, may I suggest trying out a horizontal listView library: 如果你想要一个像listview一样工作的滚动视图,我可以建议尝试一个水平的listView库:

https://github.com/sephiroth74/HorizontalVariableListView https://github.com/sephiroth74/Horizo​​ntalVariableListView

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及更高版本(并且有些调整甚至更旧)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM