0

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.

MePo
  • 1,044
  • 2
  • 23
  • 48
  • 1
    see if this topic help you http://stackoverflow.com/q/8181828/3640637 – PedroHawk Jul 16 '14 at 09:01
  • after you call: scrollViewListener.onScrollEnded(this, x, y, oldx, oldy); remove your scrollListener, so onScrollEnded will be called only once. now, when you finish getting all the data from the db, add your scroll listener again. Might not be the most elegant way but i think it will work. – Zbun Jul 17 '14 at 11:43

1 Answers1

0

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

MePo
  • 1,044
  • 2
  • 23
  • 48