Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AppBarLayout.OnOffsetChangedListener onOffsetChanged method is calling continously

I am using OnOffsetChangedListener interface to detect AppBarLayout's offset change. It working fine but the problem is onOffsetChanged method is calling continuously. How can i stop this continuous call.

   offsetChangedListener = (appBarLayout, verticalOffset) -> {
        Log.i(TAG, "onOffsetChanged: verticalOffset: " + verticalOffset);

        //  Vertical offset == 0 indicates appBar is fully expanded.
        if (Math.abs(verticalOffset) > 200) {
            appBarExpanded = false;
             invalidateOptionsMenu();
        } else {
            appBarExpanded = true;
             invalidateOptionsMenu();
        }
    };

Thanks in advance.

like image 398
Abu Yousuf Avatar asked Jul 26 '26 13:07

Abu Yousuf


1 Answers

Got the cause of the problem. Every time AppBarLayout's offset change that code is calling invalidateOptionsMenu() method. This invalidateOptionsMenu() method recreate Menu and cause AppBarLayout's offset change. So onOffsetChanged method is called again and again. I have removed invalidateOptionsMenu() method and change my logic to achieve my goal.

like image 136
Abu Yousuf Avatar answered Jul 29 '26 04:07

Abu Yousuf



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!