2014-07-16 48 views
0

我知道爲什麼我的擴展ScrollViews onScrollChanged被多次調用,當onScrollEnd達到結束時(因爲我「滾動到達」多次並因此調用onScrollEnd多次),因爲它從數據庫加載多個條目,而我希望它在第一個結束時加載它們一次。Extended ScrollView onScrollEnded從數據庫加載多個條目,但我只需要一次結束

我ExtendedScrollView類

我有

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

public interface ScrollViewListener { 

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

在我的活動我有

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

} 

那麼,有沒有辦法阻止越來越多 「端」(由滾動引起)。只有獲得OnScrollEnded一次並從DB加載6個字符串(因爲滾動其多次),然後在下12個滾動閱讀下一個6等等......

任何幫助表示讚賞。

+1

查看該主題是否可以幫助您:http://stackoverflow.com/q/8181828/3640637 – PedroHawk

+0

致電後: scrollViewListener.onScrollEnded(this,x,y,oldx,oldy); 刪除你的scrollListener,所以onScrollEnded只會被調用一次。 現在,當你完成從數據庫獲取所有數據時,再次添加滾動監聽器。 可能不是最優雅的方式,但我認爲它會起作用。 – Zbun

回答

0

我找到了一種方法結合幾個代碼,我發現這裏的StackOverflow,並得到

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

然後在我的活動

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 

    } 
}); 

此檢查其滾動的結束如果滾動停止,那麼它將運行OnScrollStopped,從而只運行一次(沒有多少次,因爲我有問題)。

+0

startScrollerTask方法怎麼樣? – NarendraJi

+0

我是以及得到錯誤 - 無法解決'setOnScrollStoppedListener':( – NarendraJi

相關問題