title: Android ListView onScroll Fires Multiple Times
body: <p>I have two views in my fragment. In the first view, I have a linear layout, which holds some controls and in the second view, I have a listview.</p>
  
  <p>I want the linear layout in the first view to be collapsed / expand when I scroll up / down listview.</p>
  
  <p>I tried to handle scroll up / down events of listview in the OnScrollListener and collapse / expand the listview.</p>
  
  <p>But there is a problem with the onScroll method,i.e, when I scroll the listview, it fires many times when I scroll the listview one time. So expanding and collapsing process of linear layout is going crazy.</p>
  
  <p>Here is - what is happening on the screen.
  <a href="https://www.youtube.com/watch?v=U7KNwS6JlUk" rel="nofollow">https://www.youtube.com/watch?v=U7KNwS6JlUk</a></p>
  
  <p>What am I doing wrong?
  What is the best way to handle scrol up / down events of listview and collapse / expand linear layout?</p>
  
  <p><strong>My Layout</strong></p>
  
  <pre><code>    &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      tools:context="com.training.mehmetyilmaz.mywallet.TransactionsActivity.TransactionsFragment"
      android:orientation="vertical"&gt;
  
  
      &lt;LinearLayout
          android:id="@+id/transactions_filter_linear_layout"
          android:layout_width="match_parent"
          android:layout_height="160dp"
          android:orientation="vertical"
          android:paddingLeft="@dimen/activity_horizontal_margin"
          android:paddingRight="@dimen/activity_horizontal_margin"
          android:paddingTop="@dimen/activity_vertical_margin"
          android:background="@color/blue"&gt;
  
          &lt;LinearLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_weight="1"
              android:orientation="horizontal"
              &gt;
  
              &lt;LinearLayout
                  android:layout_width="0dp"
                  android:layout_weight="1"
                  android:layout_height="wrap_content"
                  android:orientation="vertical"
                  &gt;
                  &lt;TextView
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:textAppearance="?android:attr/textAppearanceMedium"
                      android:text="@string/account_type_label"
                      android:textSize="@dimen/transactions_filter_text"
                      android:textColor="@color/white"
                      /&gt;
  
  
                  &lt;RadioGroup
                      android:id="@+id/transactions_account_type_radio_group"
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:orientation="horizontal"
  
                      &gt;
  
                      &lt;RadioButton
                          android:id="@+id/transactions_rd_cash"
                          android:layout_width="0dp"
                          android:layout_weight="1"
                          android:layout_height="wrap_content"
                          android:text="@string/cash"
                          android:checked="true"
                          android:textSize="@dimen/transactions_filter_text"
                          android:textColor="@color/white"
                          android:buttonTint="@color/white" /&gt;
  
                      &lt;RadioButton
                          android:id="@+id/transactions_rd_bank"
                          android:layout_width="0dp"
                          android:layout_weight="1"
                          android:layout_height="wrap_content"
                          android:text="@string/bank"
                          android:textSize="@dimen/transactions_filter_text"
                          android:textColor="@color/white"
                          android:buttonTint="@color/white"
                          /&gt;
  
                  &lt;/RadioGroup&gt;
  
              &lt;/LinearLayout&gt;&lt;!-- Money Add Type--&gt;
              &lt;LinearLayout
                  android:layout_width="0dp"
                  android:layout_weight="1"
                  android:layout_height="wrap_content"
                  android:orientation="vertical"
                  &gt;
  
                  &lt;TextView
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:text="@string/currency_label"
                      android:textAppearance="?android:attr/textAppearanceMedium"
                      android:textSize="@dimen/transactions_filter_text"
                      android:textColor="@color/white"
                      /&gt;
  
                  &lt;Spinner
                      android:id="@+id/transactions_spinner_currency"
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:entries="@array/entry_currency"
                      android:layout_gravity="right"
                      android:gravity="right|end"
                      android:textSize="@dimen/transactions_filter_text"
                      android:textColor="@color/white"
                      style="@style/DropDownColor"
                      android:background="@drawable/abc_spinner_mtrl_am_alpha"
                      &gt;
  
                  &lt;/Spinner&gt;
              &lt;/LinearLayout&gt;&lt;!-- Currency --&gt;
  
          &lt;/LinearLayout&gt;&lt;!-- First Line--&gt;
  
          &lt;View
              android:layout_width="match_parent"
              android:layout_height="1dp"
              android:background="@color/cyan_ligth"
              android:layout_marginTop="1dp"
              android:layout_marginBottom="1dp"
              &gt;
          &lt;/View&gt;
  
          &lt;LinearLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="horizontal"
              android:layout_weight="1"
              android:layout_marginTop="5dp"
              &gt;
  
              &lt;LinearLayout
                  android:layout_width="0dp"
                  android:layout_weight="1"
                  android:layout_height="wrap_content"
                  android:orientation="vertical"
                  &gt;
                  &lt;TextView
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:textAppearance="?android:attr/textAppearanceMedium"
                      android:text="@string/money_type_label"
                      android:textSize="@dimen/transactions_filter_text"
                      android:textColor="@color/white"
                      /&gt;
  
  
                  &lt;RadioGroup
                      android:id="@+id/transactions_money_type_radio_group"
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:orientation="horizontal"
                      &gt;
  
                      &lt;RadioButton
                          android:id="@+id/transactions_rd_add"
                          android:layout_width="0dp"
                          android:layout_weight="1"
                          android:layout_height="wrap_content"
                          android:text="@string/add"
                          android:textSize="@dimen/transactions_filter_text"
                          android:textColor="@color/white"
                          android:buttonTint="@color/white" /&gt;
  
                      &lt;RadioButton
                          android:id="@+id/transactions_rd_sub"
                          android:layout_width="0dp"
                          android:layout_weight="1"
                          android:layout_height="wrap_content"
                          android:text="@string/subtract"
                          android:textSize="@dimen/transactions_filter_text"
                          android:textColor="@color/white"
                          android:buttonTint="@color/white" /&gt;
  
                  &lt;/RadioGroup&gt;
  
              &lt;/LinearLayout&gt;&lt;!-- Money Type--&gt;
  
              &lt;LinearLayout
                  android:layout_width="0dp"
                  android:layout_weight="1"
                  android:layout_height="wrap_content"
                  android:orientation="vertical"
                  &gt;
  
                  &lt;TextView
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:text="@string/entry_date_label_all_dates"
                      android:textAppearance="?android:attr/textAppearanceMedium"
                      android:textSize="@dimen/transactions_filter_text"
                      android:textColor="@color/white"
                      /&gt;
  
                  &lt;Spinner
                      android:id="@+id/transactions_spinner_date"
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:entries="@array/entry_date"
  
                      android:layout_gravity="right"
                      android:gravity="right|end"
                      android:textSize="@dimen/transactions_filter_text"
                      style="@style/DropDownColor"
                      android:background="@drawable/abc_spinner_mtrl_am_alpha"&gt;
                  &lt;/Spinner&gt;
              &lt;/LinearLayout&gt;&lt;!-- Date --&gt;
  
  
          &lt;/LinearLayout&gt;&lt;!-- Second Line --&gt;
  
  
      &lt;/LinearLayout&gt;&lt;!-- Filter --&gt;
  
  
  
  &lt;RelativeLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="vertical"&gt;
  
      &lt;com.training.mehmetyilmaz.mywallet.ScrollDetectingListView
          android:layout_width="match_parent"
          android:layout_height="320dp"
          android:id="@+id/transactions_list_view"
          &gt;&lt;/com.training.mehmetyilmaz.mywallet.ScrollDetectingListView&gt;
  
  
      &lt;TextView
          android:layout_width="match_parent"
          android:layout_height="50dp"
          android:id="@+id/transactions_total_textView"
          android:text="@string/total"
          android:paddingLeft="20dp"
          android:paddingRight="20dp"
          android:paddingBottom="5dp"
          android:paddingTop="5dp"
          android:textSize="@dimen/abc_text_size_medium_material"
          android:background="@color/green"
          android:textColor="@color/white"
          android:gravity="center"
          android:layout_alignParentBottom="true"
          /&gt;
  
   &lt;/RelativeLayout&gt;
  
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p><strong>OnScrollListener</strong></p>
  
  <pre><code>mTransactionListView.setOnScrollListener(new AbsListView.OnScrollListener() {
              @Override
              public void onScrollStateChanged(AbsListView view, int scrollState) {
                  //Log.d("Scroll State", "State : " + scrollState);
              }
  
              private int mInitialScroll = 0;
  
              @Override
              public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
  
                  Log.d("Scroll", "Called");
  
                      int scrolledOffset = mTransactionListView.getVerticalScrollOffset();
  
                      if (scrolledOffset != mInitialScroll) {
  
                          boolean scrollUp = (scrolledOffset - mInitialScroll) &lt; 0;
  
                          if (scrollUp) {
                              Log.d("Scroll", "Up");
                              expand(mTransactionsFilterLinearLayout);
  
                          } else {
                              Log.d("Scroll", "Down");
                              collapse(mTransactionsFilterLinearLayout);
  
                          }
  
                          mInitialScroll = scrolledOffset;
                      }
                  }
  
          });
  </code></pre>