Android: how to check if a View inside of ScrollView is visible?

后端 未结 14 2077
自闭症患者
自闭症患者 2020-11-22 16:42

I have a ScrollView which holds a series of Views. I would like to be able to determine if a view is currently visible (if any part of it is curre

14条回答
  •  被撕碎了的回忆
    2020-11-22 17:47

    Using @Qberticus answer which was to the point but great btw, I compined a bunch of codes to check if whenever a scrollview is called and got scrolled it trigger the @Qberticus answer and you can do whatever you want, in my case I have a social network containing videos so when the view is drawed on the screen I play the video same idea like facebook and Instagram. Here's the code:

    mainscrollview.getViewTreeObserver().addOnScrollChangedListener(new OnScrollChangedListener() {
    
                        @Override
                        public void onScrollChanged() {
                            //mainscrollview is my scrollview that have inside it a linearlayout containing many child views.
                            Rect bounds = new Rect();
                             for(int xx=1;xx<=postslayoutindex;xx++)
                             {
    
                              //postslayoutindex is the index of how many posts are read.
                              //postslayoutchild is the main layout for the posts.
                            if(postslayoutchild[xx]!=null){
    
                                postslayoutchild[xx].getHitRect(bounds);
    
                            Rect scrollBounds = new Rect();
                            mainscrollview.getDrawingRect(scrollBounds);
    
                            if(Rect.intersects(scrollBounds, bounds))
                            {
                                vidPreview[xx].startPlaywithoutstoppping();
                             //I made my own custom video player using textureview and initialized it globally in the class as an array so I can access it from anywhere.
                            }
                            else
                            {
    
                            }
    
    
                            }
                        }
                        }
                    });
    

提交回复
热议问题