0

I have done the vertical scroll view.what is my requirement is,I just need to detect the end of the scroll view and make the button visible.Can anybody tell me how to detect that the scroll view has reached the bottom?

public class LockableVerScroll extends ScrollView {

private boolean isScrollable;
private ScrollListener scrollListener=null;
public LockableVerScroll(Context context)
{
    super(context);
    isScrollable=true;
}

public LockableVerScroll(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub
    isScrollable=true;
}
public LockableVerScroll(Context context, AttributeSet attrs,int defStyle)
{
    super(context, attrs, defStyle);
    isScrollable=true;
}
public boolean isScrollable()
{
    return isScrollable;
}
public boolean setScrollable(boolean mScrollable)
{
    return mScrollable=isScrollable;
}
public void setScrollViewListener(ScrollListener mscrollListener)
{
    this.scrollListener=mscrollListener;

}

need help..thanks in advance.!!

Asif Sb
  • 755
  • 9
  • 35

2 Answers2

2
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        View view = (View) getChildAt(getChildCount()-1);
        int diff = (view.getBottom()-(getHeight()+getScrollY()+view.getTop()));// Calculate the scrolldiff
        if( diff == 0 ){  // if diff is zero, then the bottom has been reached
            Log.d(ScrollTest.LOG_TAG, "MyScrollView: Bottom has been reached" );
        }
        super.onScrollChanged(l, t, oldl, oldt);
}
eLemEnt
  • 1,671
  • 12
  • 21
1

Override the onScrollChanged() method in your extension of ScrollView

@Override
protected void onScrollChanged(int l1, int t1, int l2, int t2) {

    int count = getChildCount();
    View view = (View) getChildAt(count - 1);
    int delta = (view.getBottom() - (getHeight() + getScrollY() + view.getTop()));
    if( delta == 0 )
    {
        // scrollview has reached bottom, do whatever you want here.
    }
    super.onScrollChanged(l1, t1, l2, t1);
}

Try this. This will work.

EDIT:

if( delta == 0 )
{
    // define interface in Activity / Fragment and call its method here
    mScrollListener.onScrollViewHitsBottom();
}
Y.S
  • 28,153
  • 12
  • 86
  • 113
  • Do I need to put this method in the above class or? – Asif Sb Mar 02 '15 at 05:22
  • yes add it to the `LockableVerScroll` class – Y.S Mar 02 '15 at 05:24
  • do you understand the code ? – Y.S Mar 02 '15 at 05:24
  • no....I am just a beginner.. – Asif Sb Mar 02 '15 at 05:28
  • ok no problem .... what i want to say to you is that whatever you want to do when the `ScrollView` reached bottom, you need to do it in the `if` block .... i hope you understand this :) – Y.S Mar 02 '15 at 05:30
  • that i understood...what is my doubt is I have created one custom scroll view class extends scroll view and an interface ScrollListener..I don't understand where to implement the listener...and where to add this onScrollChanged() method.. – Asif Sb Mar 02 '15 at 05:37
  • and in the `ScrollListener` interface you have defined a method for reacting when the scrollview hits bottom, am i right ?? – Y.S Mar 02 '15 at 05:42
  • right, so add the `onScrollChanged()` method to the `LockableVerScroll` class, define the interface in your `Activity` or `Fragment`, create & implement an instance of it and call its method in the `LockableVerScroll` class (see edited answer) ... – Y.S Mar 02 '15 at 05:51
  • thanks a lot...its working now!! – Asif Sb Mar 02 '15 at 06:13