滚动型检测结束 [英] Detect end of ScrollView
问题描述
我有一个应用程序,具有使用滚动型的活动。我需要当用户获取到滚动型的底部,以检测。我做了一些googleing,我发现的此页面在这里解释。但是,在这个例子中,该人延伸滚动型。正如我所说的,我向活动和必须扩展活动。
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 extend Activity and MUST extend Activity.
于是,我说:好吧,让我们尝试做延伸滚动型的自定义类,覆盖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,但是,很明显,这是行不通的。我的问题是:为什么,如果ScrollViewExt延伸滚动型,投我的脸一个ClassCastException?有没有什么办法来检测滚动的结束不会弄乱太多了?
it throws a java.lang.ClassCastException. I changed the 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它抛出一个android.view.InflateException:二进制XML文件中的行#44:错误充气类ScrollViewExt,因为它不理解的标签ScrollViewText。我不认为它有一个解决方案...
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", because it doesn't understands the tag ScrollViewText. I don't think it has a solution...
谢谢您的回答!
推荐答案
做到了!
除了修复亚历山大好心给我的,我不得不创建一个接口:
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);
}
于是,我不得不覆盖从滚动型的OnScrollChanged方法在我ScrollViewExt:
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);
}
}
}
现在,作为亚历山大说,把包名的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!
这篇关于滚动型检测结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!