Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect end of ScrollView

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.

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".

I did, but in this line:

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

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?

Thank you people.

EDIT: 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> 

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 understand the tag ScrollViewEx. I don't think it has a solution...

Thanks for your answers!

like image 404
Fustigador Avatar asked Apr 25 '12 13:04

Fustigador


People also ask

How to detect end of ScrollView android?

This example demonstrates how do I detect end of scrollView in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

How can I tell when a Uiscrollview has finished scrolling?

scrollViewDidScroll only notifies you that the scroll view did scroll not that it has finished scrolling. The other method scrollViewDidEndScrollingAnimation only seems to fire if you programmatically move the scroll view not if the user scrolls.

What is scrollEventThrottle?

scrollEventThrottle If you do not need precise scroll position tracking, set this value higher to limit the information being sent across the bridge. The default value is 0 , which results in the scroll event being sent only once each time the view is scrolled. Type. Default. number.


2 Answers

Did it!

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

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

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

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

And it worked!

Thank you very much for your help, Alexandre!

like image 200
Fustigador Avatar answered Oct 06 '22 05:10

Fustigador


I found a simple way to detect this :

   scrollView.getViewTreeObserver()        .addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {             @Override             public void onScrollChanged() {                 if (scrollView.getChildAt(0).getBottom()                      <= (scrollView.getHeight() + scrollView.getScrollY())) {                     //scroll view is at bottom                 } else {                     //scroll view is not at bottom                 }             }         }); 
  • Doesn't need to custom ScrollView.
  • Scrollview can host only one direct child, so scrollView.getChildAt(0) is okay.
  • This solution is right even the height of direct child of scroll view is match_parent or wrap_content.
like image 44
Think Twice Code Once Avatar answered Oct 06 '22 05:10

Think Twice Code Once