当onscrollchangelistener使用webview滚动时,应用在给定的scrollY上崩溃

时间:2019-02-13 10:39:15

标签: java android

对不起,英语不好

你好,我是android studio的新手,我不知道如何在webView上设置onscrollchangelistener。当我将onscrollchangelistener与webView一起使用时,在给定的scrollY值应用程序上崩溃了

在我的代码下面

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        webView.setOnScrollChangeListener(new View.OnScrollChangeListener() {
            @SuppressLint("RestrictedApi")
            @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
            @Override
            public void onScrollChange(View v, int scrollX, final int scrollY, int oldScrollX, int oldScrollY) {
           if (scrollY>200) {
               Objects.requireNonNull(((AppCompatActivity) getActivity()).getSupportActionBar()).setShowHideAnimationEnabled(true);
               Objects.requireNonNull(((AppCompatActivity) getActivity()).getSupportActionBar()).hide();
           }
           else  if (scrollY==0)
           {
               Objects.requireNonNull(((AppCompatActivity) getActivity()).getSupportActionBar()).setShowHideAnimationEnabled(true);
               Objects.requireNonNull(((AppCompatActivity) getActivity()).getSupportActionBar()).show();
           }
           else
           {
               Objects.requireNonNull(((AppCompatActivity) getActivity()).getSupportActionBar()).setShowHideAnimationEnabled(true);
               Objects.requireNonNull(((AppCompatActivity) getActivity()).getSupportActionBar()).show();
           }
            }
        });
    }

感谢希望能帮助stackOverFlow

1 个答案:

答案 0 :(得分:0)

boolean isAnimated = false;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        webView.setOnScrollChangeListener(new View.OnScrollChangeListener() {
            @SuppressLint("RestrictedApi")
            @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
            @Override
            public void onScrollChange(View v, int scrollX, final int scrollY, int oldScrollX, int oldScrollY) {
           if (scrollY>200 && !isAnimated ) {
               isAnimated = true;
               Objects.requireNonNull(((AppCompatActivity) getActivity()).getSupportActionBar()).setShowHideAnimationEnabled(true);
               Objects.requireNonNull(((AppCompatActivity) getActivity()).getSupportActionBar()).hide();
           }
           else  if (scrollY==0)
           {
               isAnimated = false;
               Objects.requireNonNull(((AppCompatActivity) getActivity()).getSupportActionBar()).setShowHideAnimationEnabled(true);
               Objects.requireNonNull(((AppCompatActivity) getActivity()).getSupportActionBar()).show();
           }
           else
           {
               Objects.requireNonNull(((AppCompatActivity) getActivity()).getSupportActionBar()).setShowHideAnimationEnabled(true);
               Objects.requireNonNull(((AppCompatActivity) getActivity()).getSupportActionBar()).show();
           }
            }
        });
    }