rem*_*til 8 android scrollview android-scrollview onscrolllistener onscrollchanged
我已经实现了一个侦听器类来检测滚动视图的结尾,参考链接https://gist.github.com/marteinn/9427072
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);
}
}
Run Code Online (Sandbox Code Playgroud)
监听器设置为scrollView:
scrollView.setBottomReachesListener(new GenericScrollListerner(this));
Run Code Online (Sandbox Code Playgroud)
我的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();
}
Run Code Online (Sandbox Code Playgroud)
}
我的问题是onBottomReached在大多数情况下都会触发两次.如何处理这个问题???
这是由于过度滚动而发生的.请在scrollview xml声明中添加以下行
android:overScrollMode="never"
Run Code Online (Sandbox Code Playgroud)
如果问题仍然存在,请使用以下调整
@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);
}
Run Code Online (Sandbox Code Playgroud)
最后,我没有经过任何调整就找到了答案。
使用“NestedScrollView”而不是 ScrollView 扩展自定义滚动视图
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);
}
}
Run Code Online (Sandbox Code Playgroud)
上面的代码将起作用
| 归档时间: |
|
| 查看次数: |
1122 次 |
| 最近记录: |