Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onOffsetChanged method from OnOffsetChangedListener of AppBarLayout being constantly called

I have been looking for an answer to this for days. There's not a single thing online I can use.

My problem is that when adding an OnOffsetChangedListener to the AppBarLayout, its method (onOffsetChaned) gets called over and over again, even if I'm not touching the screen. Aditionally, the values of verticalOffset never change, nor does the getHeight() value returned by the Toolbar or the AbbBarLayout.

I think it's important to add that this happens also using a master detail flow template from Android Studio without changing anything other than adding the code to the activity, so I don't think it's something in my code.

Thanks in advance.

This is the code I'm using

appbarLayout = (AppBarLayout) findViewById(R.id.app_bar);
    appbarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
        @Override
        public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
            showToast(String.valueOf(appBarLayout.getHeight()));
            if (Math.abs(verticalOffset) == appBarLayout.getTotalScrollRange()) {
                showToast("Collapsed");
            } else if (verticalOffset == 0) {
                showToast("Expanded");
            } else {
               showToast("Scrolling");
            }
        }
    });

And this is my XML file

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".views.ActivityTaskList"
    tools:ignore="MergeRootFrame">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="170dp"
        android:fitsSystemWindows="true"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleGravity="start|top"
            app:expandedTitleMarginTop="16dp"
            app:expandedTitleMarginStart="20dp"
            app:expandedTitleTextAppearance="@style/TextAppearance.AppCompat.Title"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"

            app:toolbarId="@+id/toolbar">

            <android.support.constraint.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <EditText
                    android:id="@+id/input_layout_task"
                    style="@style/Widget.AppCompat.EditText"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginEnd="8dp"
                    android:layout_marginStart="72dp"
                    android:layout_marginTop="48dp"
                    android:hint="I need to..."
                    android:textAppearance="@style/TextAppearance.AppCompat"
                    android:theme="@style/ToolbarEditText"
                    app:layout_constraintHorizontal_bias="0.0"
                    app:layout_constraintLeft_toLeftOf="parent"
                    app:layout_constraintRight_toRightOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />


                <TextView
                    android:id="@+id/tvDatePicker"
                    style="@style/Base.Widget.AppCompat.Spinner"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_alignParentTop="true"
                    android:layout_marginStart="4dp"
                    android:layout_marginTop="0dp"
                    android:paddingTop="16dp"
                    android:text="Fecha"
                    app:layout_constraintHorizontal_bias="0.1"
                    app:layout_constraintLeft_toLeftOf="@+id/input_layout_task"
                    app:layout_constraintRight_toRightOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/input_layout_task" />

                <ImageView
                    android:id="@+id/imageView2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="0dp"
                    android:layout_marginLeft="16dp"
                    android:layout_marginTop="0dp"
                    app:layout_constraintBottom_toTopOf="@+id/tvDatePicker"
                    app:layout_constraintLeft_toLeftOf="parent"
                    app:layout_constraintTop_toTopOf="@+id/input_layout_task"
                    app:layout_constraintVertical_bias="0.44"
                    app:srcCompat="@drawable/ic_label_black_24dp" />

                <ImageView
                    android:id="@+id/imageView3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="0dp"
                    android:layout_marginEnd="32dp"
                    android:layout_marginStart="14dp"
                    android:layout_marginTop="8dp"
                    app:layout_constraintBottom_toBottomOf="@+id/tvDatePicker"
                    app:layout_constraintLeft_toLeftOf="parent"
                    app:layout_constraintRight_toLeftOf="@+id/tvDatePicker"
                    app:layout_constraintTop_toTopOf="@+id/tvDatePicker"
                    app:layout_constraintVertical_bias="1.0"
                    app:srcCompat="@drawable/ic_date_range_black_24dp" />


            </android.support.constraint.ConstraintLayout>

            <android.support.v7.widget.Toolbar
                android:id="@+id/detail_toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

        </android.support.design.widget.CollapsingToolbarLayout>


    </android.support.design.widget.AppBarLayout>

    <FrameLayout
        android:id="@+id/frameLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <include layout="@layout/task_list" />
    </FrameLayout>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@drawable/ic_add_black_24dp"
        android:tint="@android:color/white"
        app:fabSize="normal" />


</android.support.design.widget.CoordinatorLayout>
like image 363
Adrian Flores Avatar asked Nov 19 '22 02:11

Adrian Flores


1 Answers

It's too late but I'll answer in case anyone stumbles upon this issue.

It's being called every time because there is something being updated inside your layout constantly.

For example, if you have a TextView inside AppBarLayout, which you update every second (something like a countdown timer), every time the onOffsetChanged method will be called, but you'll see inside that the verticalOffset indeed is 0.

In your case it could be due to the EditText cursor blinking every second.

like image 173
Ahmadul Hoq Avatar answered Dec 16 '22 01:12

Ahmadul Hoq