2

I have to listen for the position change of a particular view. The view can be anywhere , like , it can be an item of the listview or recycler view , or it can be a child of a scrollview or nested scrollview etc. But whenever the position in the screen of the view has been changed (Whenever the x,y coordinates being updated ), I need to be notified. I tried

view.setOnScrollChangeListener(new View.OnScrollChangeListener() {
        @Override
        public void onScrollChange(View view, int i, int i1, int i2, int i3) {
            Log.d(TAG, "Position changed");
        }
    });

and

view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            Log.d(TAG, "Layout changed");
        }
    });

But doesn't seem to work

I tried GestureDetectorCompat also , But during fling , the x,y positions are not accurate.

Is there any solution for my issue? Hope my issue is clear. Thanks in advance!!

  • I think you should tell us what you're actually trying to accomplish. Because this does not seem to be the right way to do it. – Gabe Sechan Feb 19 '17 at 15:08
  • Basically I have 2 views. One is my toolbar and the other one can be anywhere in the activity(or fragment) below the toolbar. So whenever this bottom view come closer to the toolbar , I need to change the intensity of the texts and backgrounds in the toolbar(Such as changing the alpha of the toolbar title and fading the toolbar background color). Also whenever it is moving away from the toolbar. So always I need to know this view is how much distance ahead from the toolbar. –  Feb 19 '17 at 15:24
  • Also This view can be anywhere in the screen , such as title of a list view , or a normal linear layout etc. Based on different screens. Does it help you to understand my requirement? –  Feb 19 '17 at 15:26
  • Somewhat, although I'm having a hard time picturing why you need to do this in such a generic way. ANd I don't think its actually possible- the system isn't set up for it. Views don't really know where they are on the screen. The global layout listener will only be called when its laid out. The scroll listener is only for scrolling of the views own contents. There is no view has moved callback. I can think of some hacks, but they'd all involve custom views. – Gabe Sechan Feb 19 '17 at 15:32
  • What about making the below view inside a custom view? Will it help? If I wrap my below view inside my custom view group? –  Feb 19 '17 at 15:38
  • Then I can come up with some extremely ugly hacks. But I'm not even 100% sure they'll work under all circumstances. The way Android works a view just isn't supposed to care where on the screen it is. It only needs to care about drawing to its own canvas. For things like scrolling it may not even need to redraw, as in cases of simple scrolling it may be done via matrix manipulation of the parent. – Gabe Sechan Feb 19 '17 at 15:41
  • One thing is fixed in all the screens that , the bottom view will be inside any of the following. a listview , recycler view, scrollview or a nested scrollview. Means the very next view under the toolbar will be anyone the above 4. So my second view (the one which I want to listen movement) will be a child of any of these 4. Is there any common scroll listener that is applicable for all these scrollable views? –  Feb 19 '17 at 15:49

1 Answers1

2

I think you can make use of ViewTreeObserver.OnScrollChangedListener callback. This is the callback invoked when something in the view tree has been scrolled. So try adding this listener for your view which is moving.

view.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
        @Override
        public void onScrollChanged() {
            // Do what you want to do when the view has been scrolled
        }
    });
Sarath Kn
  • 2,680
  • 19
  • 24
  • Great!! This is exactly what I wanted. Now I could trace the movement using this callback. Thanks!! –  Feb 22 '17 at 07:29