我知道为什么在使用onScrollEnd结束时会多次调用我的Extended ScrollViews onScrollChanged(这是因为我通过滚动多次“到达结束”,从而多次调用onScrollEnd),并且因为它在我需要时从数据库加载多个条目到达第一端时将其加载一次。
在我的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并从数据库中仅加载6个字符串(由于多次滚动调用),然后在接下来的12个Scroll中读取接下来的6个,依此类推...
任何帮助表示赞赏。
我找到了一种在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
}
});
这将检查其滚动是否结束以及Scroll是否停止,然后运行OnScrollStopped,因此仅运行一次(不多于我之前遇到的问题)。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句