ScrollView上的setOnScrollChangeListener:错误

时间:2016-09-19 16:48:50

标签: android compiler-errors scrollview

当用户位于ScrollView的末尾时,我想检测它并加载一些新项目。我已经完成了,但似乎我使用的功能不适用于某些旧版本的Android。

我有这段代码:

scrollView = (ScrollView) findViewById(R.id.scrollView);
    scrollView.setOnScrollChangeListener(new ScrollListener(this));

然后:

@TargetApi(Build.VERSION_CODES.M)
public class ScrollListener implements View.OnScrollChangeListener {


ScrollingActivity context;

public ScrollListener(ScrollingActivity context) {
    this.context = context;
}

@Override
public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
    ScrollView scrollView = (ScrollView) v;
    View view = (View) scrollView.getChildAt(scrollView.getChildCount() - 1);
    int diff = (view.getBottom() - (scrollView.getHeight() + scrollY));
    int oldDiff = (view.getBottom() - (scrollView.getHeight() + oldScrollY));
    // Si on a atteint le bas de la scrollView et qu'on était en train de descendre
    if(diff == 0 && oldDiff > 0) {
        context.endOfScroll(); // function I call to load data...
    }
}}

我有这个错误:

08-19 21:27:35.135 26504-26504/com.histoire_horreur E/AndroidRuntime: FATAL EXCEPTION: main
                                                              java.lang.NoClassDefFoundError: com.histoire_horreur.Listener.ScrollListener
                                                                  at com.histoire_horreur.MainActivity.createView(MainActivity.java:59)
                                                                  at com.histoire_horreur.MainActivity.onCreate(MainActivity.java:47)
                                                                  at android.app.Activity.performCreate(Activity.java:5122)
                                                                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1150)
                                                                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2315)
                                                                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2403)
                                                                  at android.app.ActivityThread.access$600(ActivityThread.java:165)
                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1373)
                                                                  at android.os.Handler.dispatchMessage(Handler.java:107)
                                                                  at android.os.Looper.loop(Looper.java:194)
                                                                  at android.app.ActivityThread.main(ActivityThread.java:5391)
                                                                  at java.lang.reflect.Method.invokeNative(Native Method)
                                                                  at java.lang.reflect.Method.invoke(Method.java:525)
                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
                                                                  at dalvik.system.NativeStart.main(Native Method)

正如我所说,这段代码适用于某些版本的Android,但对于我的Wiko 4.2.2却没有。 我的错误与此处相同:https://forums.xamarin.com/discussion/62241/problem-setting-on-scroll-listener-to-android-horizontal-scroll-view-class-not-found-exception 解决方案是使用ViewTreeObserver,但是,我没有成功检测到滚动位于底部的时间。我试着这样做:

scrollView = (ScrollView) findViewById(R.id.scrollView);
scrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
@Override
public void onScrollChanged() {
// How to get X, Y, Old X and Old Y ?
}
});

但是我无法获得scrollX,scrollY,oldScrollX和oldScrollY。

你知道如何让它适用于Android的所有版本吗?

1 个答案:

答案 0 :(得分:2)

我有同样的问题。 如果你使用ViewTreeObserver代码正常工作,没有异常,只有在底部滚动视图时才会检测到问题。 这段剪辑代码可以帮助您:

@Override
public void onScrollChanged() {

    View v = myScrollView.getChildAt(myScrollView.getChildCount() - 1);
    int diff = (v.getBottom() - (myScrollView.getHeight() + myScrollView.getScrollY()));
    if (diff <= 0) {
        //load data here, there is bottom of scrollview
     }
}