Search code examples
androidscrollviewonscrolllistener

Extended ScrollView onScrollEnded Loading multiple entries from Database but i only need that one time per End


I know why my Extended ScrollViews onScrollChanged is being called multiple times when end is reached with onScrollEnd (Its because I "reach end" multiple times with scrolling and thus calling onScrollEnd multiple times) and because of that its Loading multiple entries from Database while I want it to Load them once when first end is reached.

in my ExtendedScrollView class i have

protected void onScrollChanged(int x, int y, int oldx, int oldy) {

View view = (View) getChildAt(getChildCount() - 1);
int diff = (view.getBottom()-(getHeight()+getScrollY()+view.getTop()));

if (diff == 0) { 

      if (scrollViewListener != null) {

          scrollViewListener.onScrollEnded(this, x, y, oldx, oldy);

      }

  }

   super.onScrollChanged(x, y, oldx, oldy);
}

with

public interface ScrollViewListener {

   void onScrollEnded(ExtendedScrollView scrollView, int x, int y, int oldx, int oldy);
}

In my Activity I have

@Override
    public void onScrollEnded(ExtendedScrollView scrollView, int x, int y, int oldx, int oldy) {

   //Called multiple times because of scroll but needed only once 

  //read 6 Strings (for example) from Database and refresh Tableview with that data (working so I think there is no need for the code)

}

So is there a way to stop getting multiple "ends" (caused by scrolling). Only to get the OnScrollEnded Once and load only 6 Strings from DB (Because of scrolling its called multiple times), then on the next 12 Scroll read next 6 and so on...

Any help is appreciated.


Solution

  • I found a way Combined couple of code I found here at StackOverflow and got

    ExtendedViewClass

    public class ExtendedScrollView extends ScrollView {
    
    private Runnable scrollerTask;
    private int initialPosition;
    
    private int newCheck = 100;
    
    
    public interface OnScrollStoppedListener{
    void onScrollStopped();
    }
    
    private OnScrollStoppedListener onScrollStoppedListener;
    
    public ExtendedScrollView(Context context, AttributeSet attrs) {
    super(context, attrs);
    
    scrollerTask = new Runnable() {
    
        public void run() {
    
            int newPosition = getScrollY();
    
            View view = (View) getChildAt(getChildCount() - 1);
            int diff = (view.getBottom()-(getHeight()+getScrollY()+view.getTop()));
    
            if(initialPosition - newPosition == 0 && diff == 0){//has stopped and reached end
    
                if(onScrollStoppedListener!=null){
    
                    onScrollStoppedListener.onScrollStopped();
                }
            }
        }
     };
    }
    
    public void setOnScrollStoppedListener(ExtendedScrollView.OnScrollStoppedListener listener){
    onScrollStoppedListener = listener;
     }
    
    public void startScrollerTask(){
    
    initialPosition = getScrollY();
    ExtendedScrollView.this.postDelayed(scrollerTask, newCheck);
      }
    }
    

    And then in my Activity

    scrollView = (ExtendedScrollView) findViewById(R.id.ScrView);
    
    
    scrollView.setOnTouchListener(new OnTouchListener() {
    
        public boolean onTouch(View v, MotionEvent event) {
    
            if (event.getAction() == MotionEvent.ACTION_UP) {
    
    
                scrollView.startScrollerTask();
            }
    
            return false;
        }
    });
    scrollView.setOnScrollStoppedListener(new OnScrollStoppedListener() {
    
        public void onScrollStopped() {
    
            //code here
    
        }
    });
    

    This checks if its end of scroll and if Scroll stops then it run OnScrollStopped thus running it only once (not many times as I had the problem before).