0

I have a LinearLayout (vertical) encapsulated in a ScrollView. The LinearLayout is load dynamically, and I want to load more data when page bottom is reached after scrolling.

To know when the page bottom is reached, I overrided ScrollView.onScrollChanged() method :

public class VerticalScrollView extends ScrollView {

    private IVerticalScrollListener listener;
    private LinearLayout list;

    public VerticalScrollView(Context context, AttributeSet attributes) {
        super(context, attributes);
    }

    public void setListener(IVerticalScrollListener listener) {
        this.listener = listener;
    }

    @Override
    protected void onScrollChanged(int x, int y, int oldx, int oldy) {
        super.onScrollChanged(x, y, oldx, oldy);

        if (list == null) {
            list = (LinearLayout) findViewById(R.id.list);
        }

        final int bottom = list.getMeasuredHeight();

        if (y >= bottom) { //never occures
            listener.onPageBottomReached();
        }
    }

}

The trouble is that the "y" value is always (far) less than LinearLayout height.

Why ? Thank you.

hadf
  • 228
  • 5
  • 15

2 Answers2

0

You shouldn't detect this by the height. Instead, you should detect the item that is currently on the user's screen.

This link might help.

The Hungry Androider
  • 2,158
  • 5
  • 24
  • 45
  • Thank you for your reply. How to use ListView http://www.vogella.com/tutorials/AndroidListView/article.html – hadf Mar 30 '15 at 19:53
0

Code that I use to detect the end of scrolling. The timer is so the end event does not fire repeatedly when the end is reached.

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.ScrollView;

public class EndDetectableScrollView extends ScrollView {

   private long mLastEndCall = 0;
   private ScrollViewListener scrollViewListener = null;

   public EndDetectableScrollView(Context context) {
      super(context);

   }

   public EndDetectableScrollView(Context context, AttributeSet attrs) {
      super(context, attrs);

   }

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


   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) {
        View view = (View) getChildAt(0);
           int diff = (view.getBottom()-(getHeight()+getScrollY()+view.getTop()));
           if ( diff <= 0 ) {  
              long currentTime = System.currentTimeMillis();
              if ( currentTime > mLastEndCall + 1000)
              {
                 Log.d("TAG","CurrentTime: " + currentTime + " lastTime: " + mLastEndCall);
                 mLastEndCall = currentTime;
                 scrollViewListener.onScrollEnd();
              }
           }

       }
   }
}
Gary Bak
  • 4,564
  • 4
  • 19
  • 32