如何检查片段中的视图出现在当前 window 中还是不在当前 window 中?

How to check a view in fragment is appearing in the current window or it's out of the current window?

我里面有滚动视图和文本视图,我想知道它是离开了window还是还在window里面。

scrollView.setOnScrollChangeListener(new View.OnScrollChangeListener() {
                @Override
                public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                    L.e(" isShown " + textView.isShown());
                }
            });

提前致谢。

使用下面的代码。它非常适合我

if (scrollView != null) {
       if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
           Rect scrollBounds = new Rect();
           scrollView.getHitRect(scrollBounds);
           scrollView.setOnScrollChangeListener(new View.OnScrollChangeListener() {
               @Override
               public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                   String title = "your title";
                   if (view != null) {
                       if (view.getLocalVisibleRect(scrollBounds)) {
                           setPageTitle("");
                       } else {
                           setPageTitle(title == null ? "" : title);
                       }
                   }
               }
           });
       }
   }