title: Android : Hide RelativeLayout with animationwhen scroll down / up listview
body: <p>I try to implement an animation for hiding a relativeLayout which is above a listview. I would like to have this behaviour :</p>
  
  <ul>
  <li>on Scroll DOWN: Hide RelativeLayout</li>
  <li>on Scroll UP : Show RelativeLayout only when i am at the top</li>
  </ul>
  
  <p><img src="https://i.stack.imgur.com/fXa7x.jpg" alt="enter image description here"></p>
  
  <p>The following code that i have created works, but there is 2 problems :</p>
  
  <ol>
  <li>When i scroll down or when i scroll up, if i am at the top of the listview, there is a blink of the relativelayout. I must perform a fast scroll in order to hide this climb. => There is a problem in my code, maybe on listIsAtTop() function ?</li>
  <li>And second problem, i don't have animation. I use setVisivility(VISIBLE or GONE) to show or hide the relativeLayout. It's not user friendly, isn't it ? :) I don't know how to add a little translation animation to hide and show this relativeLayout.</li>
  </ol>
  
  <p><strong>in onCreate() of Account_Activity class :</strong></p>
  
  <pre><code>list = (com.app.frisbeee.account_view.CustomListView)findViewById(R.id.accountview_listview);
  adapter = new Account_view_custom_adapter(Account_view_act.this, annoncesList);
              list.setAdapter(adapter);
              adapter.notifyDataSetChanged();
              registerForContextMenu(list);
  
              list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                  @Override
                  public void onItemClick(AdapterView&lt;?&gt; parent, View view, int position, long id) {
                      list.showContextMenuForChild(view);   
                  }
              });
  
              list.setOnDetectScrollListener(new OnDetectScrollListener() {
                  @Override
                  public void onUpScrolling() {
                      /* do something */
                      Log.d("INFO", "UPPPPPPPPPPP");
                      if (listIsAtTop()) {
                          RelativeLayout relative1 = (RelativeLayout) findViewById(R.id.relative1);
                          relative1.setVisibility(View.VISIBLE);
                      }
                  }
  
                  @Override
                  public void onDownScrolling() {
                      /* do something */
                      Log.d("INFO", "DOWNNNNNNNN");
                      if (!listIsAtTop()) {
                          RelativeLayout relative1 = (RelativeLayout) findViewById(R.id.relative1);
                          relative1.setVisibility(View.GONE);
                      }
  
                  }
              });
  
          }
  </code></pre>
  
  <p><strong>Code of listIsAtTop function :</strong></p>
  
  <pre><code>private boolean listIsAtTop()   {   
          if (list.getChildCount() == 0) {
              return true;
          }
          return list.getFirstVisiblePosition() == 0 &amp;&amp; (list.getChildCount() == 0 || list.getChildAt(0).getTop() == 0);
      }
  </code></pre>
  
  <p><strong>CustomListView</strong> used to detect scroll UP and DOWN :</p>
  
  <pre><code>import android.content.Context;
  import android.util.AttributeSet;
  import android.view.View;
  import android.widget.AbsListView;
  
  public class CustomListView extends android.widget.ListView {
  
      private OnScrollListener onScrollListener;
      private OnDetectScrollListener onDetectScrollListener;
  
      public CustomListView(Context context) {
          super(context);
          onCreate(context, null, null);
      }
  
      public CustomListView(Context context, AttributeSet attrs) {
          super(context, attrs);
          onCreate(context, attrs, null);
      }
  
      public CustomListView(Context context, AttributeSet attrs, int defStyle) {
          super(context, attrs, defStyle);
          onCreate(context, attrs, defStyle);
      }
  
      private void onCreate(Context context, AttributeSet attrs, Integer defStyle) {
          setListeners();
      }
  
      private void setListeners() {
          super.setOnScrollListener(new OnScrollListener() {
  
              private int oldTop;
              private int oldFirstVisibleItem;
  
              @Override
              public void onScrollStateChanged(AbsListView view, int scrollState) {
                  if (onScrollListener != null) {
                      onScrollListener.onScrollStateChanged(view, scrollState);
                  }
              }
  
              @Override
              public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                  if (onScrollListener != null) {
                      onScrollListener.onScroll(view, firstVisibleItem, visibleItemCount, totalItemCount);
                  }
  
                  if (onDetectScrollListener != null) {
                      onDetectedListScroll(view, firstVisibleItem);
                  }
              }
  
              private void onDetectedListScroll(AbsListView absListView, int firstVisibleItem) {
                  View view = absListView.getChildAt(0);
                  int top = (view == null) ? 0 : view.getTop();
  
                  if (firstVisibleItem == oldFirstVisibleItem) {
                      if (top &gt; oldTop) {
                          onDetectScrollListener.onUpScrolling();
                      } else if (top &lt; oldTop) {
                          onDetectScrollListener.onDownScrolling();
                      }
                  } else {
                      if (firstVisibleItem &lt; oldFirstVisibleItem) {
                          onDetectScrollListener.onUpScrolling();
                      } else {
                          onDetectScrollListener.onDownScrolling();
                      }
                  }
  
                  oldTop = top;
                  oldFirstVisibleItem = firstVisibleItem;
              }
          });
      }
  
      @Override
      public void setOnScrollListener(OnScrollListener onScrollListener) {
          this.onScrollListener = onScrollListener;
      }
  
      public void setOnDetectScrollListener(OnDetectScrollListener onDetectScrollListener) {
          this.onDetectScrollListener = onDetectScrollListener;
      }
  }
  </code></pre>