检测 ScrollView 的结束 [英] Detect end of ScrollView

查看:40
本文介绍了检测 ScrollView 的结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,它有一个使用 ScrollView 的 Activity.我需要检测用户何时到达 ScrollView 的底部.我做了一些谷歌搜索,我找到了 这个页面 的解释.但是,在这个例子中,那些家伙扩展了 ScrollView.正如我所说,我需要扩展 Activity.

I have an app, that has an Activity that uses a ScrollView. I need to detect when user gets to the bottom of the ScrollView. I did some googleing and I found this page where is explained. But, in the example, that guys extends ScrollView. As I said, I need to extend Activity.

所以,我说好吧,让我们尝试制作一个扩展 ScrollView 的自定义类,覆盖 onScrollChanged() 方法,检测滚动的结尾,然后执行相应".

So, I said "ok, let's try to make a custom class extending ScrollView, override the onScrollChanged() method, detect the end of the scroll, and act accordingly".

我做了,但在这一行:

scroll = (ScrollViewExt) findViewById(R.id.scrollView1);

它抛出一个 java.lang.ClassCastException.我更改了 XML 中的 <ScrollView> 标记,但很明显,它不起作用.我的问题是:如果 ScrollViewExt 扩展 ScrollView,为什么会向我抛出 ClassCastException?有没有什么方法可以检测滚动结束而不会弄乱太多?

it throws a java.lang.ClassCastException. I changed the <ScrollView> tags in my XML but, obviously, it doesn't work. My questions are: Why, if ScrollViewExt extends ScrollView, throws to my face a ClassCastException? is there any way to detect end of scrolling without messing too much?

谢谢大家.

正如所承诺的,这是我的 XML 部分很重要:

As promised, here is the piece of my XML that matters:

<ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >


        <WebView
            android:id="@+id/textterms"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center_horizontal"
            android:textColor="@android:color/black" />

    </ScrollView>

我将它从 TextView 更改为 WebView 以便能够对齐里面的文本.我想要实现的是在完全阅读合同条款之前不会激活接受按钮"的事情.我的扩展类称为 ScrollViewExt.如果我为 ScrollViewExt 更改标签 ScrollView,它会抛出一个

I changed it from TextView to WebView to be able of justifying the text inside. What i want to achieve is the "Accept button doesn't activate until the terms of the contract are fully read" thing. My extended class is called ScrollViewExt. If i change the tag ScrollView for ScrollViewExt it throws an

android.view.InflateException: Binary XML file line #44: Error inflating class ScrollViewExt

因为它不理解标签 ScrollViewEx.我不认为它有解决方案...

because it doesn't understand the tag ScrollViewEx. I don't think it has a solution...

感谢您的回答!

推荐答案

做到了!

除了 Alexandre 为我提供的修复之外,我还必须创建一个接口:

Aside of the fix Alexandre kindly provide me, I had to create an Interface:

public interface ScrollViewListener {
    void onScrollChanged(ScrollViewExt scrollView, 
                         int x, int y, int oldx, int oldy);
}

然后,我不得不在我的 ScrollViewExt 中覆盖来自 ScrollView 的 OnScrollChanged 方法:

Then, i had to override the OnScrollChanged method from ScrollView in my ScrollViewExt:

public class ScrollViewExt extends ScrollView {
    private ScrollViewListener scrollViewListener = null;
    public ScrollViewExt(Context context) {
        super(context);
    }

    public ScrollViewExt(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public ScrollViewExt(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void setScrollViewListener(ScrollViewListener scrollViewListener) {
        this.scrollViewListener = scrollViewListener;
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        if (scrollViewListener != null) {
            scrollViewListener.onScrollChanged(this, l, t, oldl, oldt);
        }
    }
}

现在,正如Alexandre所说,把包名放在XML标签中(我的错),让我的Activity类实现之前创建的接口,然后,把它们放在一起:

Now, as Alexandre said, put the package name in the XML tag (my fault), make my Activity class implement the interface created before, and then, put it all together:

scroll = (ScrollViewExt) findViewById(R.id.scrollView1);
scroll.setScrollViewListener(this);

而在方法 OnScrollChanged 中,从界面...

And in the method OnScrollChanged, from the interface...

@Override
public void onScrollChanged(ScrollViewExt scrollView, int x, int y, int oldx, int oldy) {
    // We take the last son in the scrollview
    View view = (View) scrollView.getChildAt(scrollView.getChildCount() - 1);
    int diff = (view.getBottom() - (scrollView.getHeight() + scrollView.getScrollY()));

    // if diff is zero, then the bottom has been reached
    if (diff == 0) {
        // do stuff
    }
}

它奏效了!

非常感谢您的帮助,亚历山大!

Thank you very much for your help, Alexandre!

这篇关于检测 ScrollView 的结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆