6

我實現了一個監聽器類,以檢測滾動視圖的到底指的鏈接https://gist.github.com/marteinn/9427072onScrollChanged燒製滾動結束多次滾動型

public class ResponsiveScrollView extends ScrollView { 

private OnBottomReachedListener listener; 

public ResponsiveScrollView(Context context) { 
    super(context); 
} 

@Override 
protected void onScrollChanged(int l, int t, int oldl, int oldt) { 
    View view = getChildAt(getChildCount()-1); 
    int diff = view.getBottom()-(getHeight()+getScrollY()); 
    if (diff == 0 && listener!= null) { 
     listener.onBottomReached(this); 
    } 
    super.onScrollChanged(l, t, oldl, oldt); 
} 

public OnBottomReachedListener getBottomChangedListener() { 
     return listener; 
} 

public void setBottomReachesListener(OnBottomReachedListener onBottomReachedListener) { 
    this.listener = onBottomReachedListener; 
} 

public interface OnBottomReachedListener { 
    public void onBottomReached(View view); 
    } 
} 

監聽器設置爲滾動視圖:

scrollView.setBottomReachesListener(new GenericScrollListerner(this)); 

我GenericScrollListerner等級:

public class GenericScrollListerner implements ResponsiveScrollView.OnBottomReachedListener { 
private Context mContext; 

public GenericScrollListerner(Context context) { 
    this.mContext = context; 
} 

@Override 
public void onBottomReached(View view) { 
    Log.d("ScrollView","Scroll end"); 
    String tag = (String) view.getTag(); 
    Toast.makeText(mContext, "Scroll end with tag" +tag, Toast.LENGTH_SHORT).show(); 
} 

}

我的問題是onBottomReached正在兩次大部分時間發射。如何處理這個問題?

回答

1

最後,我發現沒有一個調整一個答案。

擴展定製滾動視圖與 'NestedScrollView',而不是滾動型

public class ResponsiveScrollView extends NestedScrollView { 

private OnBottomReachedListener listener; 

public ResponsiveScrollView(Context context) { 
    super(context); 
} 

@Override 
protected void onScrollChanged(int l, int t, int oldl, int oldt) { 
    View view = getChildAt(getChildCount()-1); 
    int diff = view.getBottom()-(getHeight()+getScrollY()); 
    if (diff == 0 && listener!= null) { 
     listener.onBottomReached(this); 
    } 
    super.onScrollChanged(l, t, oldl, oldt); 
} 

public OnBottomReachedListener getBottomChangedListener() { 
     return listener; 
} 

public void setBottomReachesListener(OnBottomReachedListener onBottomReachedListener) { 
    this.listener = onBottomReachedListener; 
} 

public interface OnBottomReachedListener { 
    public void onBottomReached(View view); 
    } 
} 

上面的代碼將工作

4

這是由於過卷。請添加以下行滾動型XML聲明

android:overScrollMode="never" 

如果問題仍然存在,然後使用下面的調整

@Override 
    protected void onScrollChanged(int l, int t, int oldl, int oldt) { 
     View view = getChildAt(getChildCount() - 1); 
     int diff = view.getBottom() - (getHeight() + getScrollY()); 
     if (diff == 0 && listener != null && !stopTwice) { 
      stopTwice=true; 
      listener.onBottomReached(this); 
     }else{ 
      stopTwice=false; 
     } 
     super.onScrollChanged(l, t, oldl, oldt); 
    } 
+0

感謝。它的工作原理 –

+0

哪一個工作? xml聲明還是調整? @remyathekkuvettil – Nizam

+0

調整爲我工作@Nizam –