6

How to hide/show view when scrolling up/down android like Foodpanda app

enter image description here

I want to hide/show view (linear or relative layout) when ScrollView is up/down like this above gif.

But my app I do not use Recyclerview or list view (just textview).

How can I create it?

Thanks!

Kaiser
  • 606
  • 8
  • 22
f982
  • 97
  • 1
  • 4
  • You are looking for CoordinatorLayout. – Karol Kulbaka Feb 24 '18 at 10:05
  • see below link https://stackoverflow.com/questions/57308572/collapsingtoolbarlayout-with-fixed-pinned-toolbar-and-functionality-of-enteralw – A.Hosein Sep 23 '20 at 08:18
  • see the link, I found my answer [here](https://stackoverflow.com/questions/57308572/collapsingtoolbarlayout-with-fixed-pinned-toolbar-and-functionality-of-enteralw) – A.Hosein Sep 23 '20 at 08:20

1 Answers1

10

Add scroll listener to the RecylerView

  1. If the user is scrolling down - then start translation animation UPWARDS

  2. If the user is scrolling up - then start translation animation DOWNWARDS

Anim translation UPWARDS:- (trans_upwards.xml)

<?xml version="1.0" encoding="utf-8"?>
<set
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/linear_interpolator"
    android:fillAfter="true">

    <translate
        android:fromYDelta="0%p"
        android:toYDelta="100%p"
        android:duration="300"
         />

</set>

Anim translation DOWNWARDS:-(trans_downwards.xml)

<?xml version="1.0" encoding="utf-8"?>
<set
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/linear_interpolator"
    android:fillAfter="true">

    <translate
        android:fromYDelta="100%p"
        android:toYDelta="0%p"
        android:duration="300"
         />

</set>

Add scroll listener to recyclerView (and also do a checking)

boolean check_ScrollingUp = false;
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {

@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
    super.onScrolled(recyclerView, dx, dy);
    if (dy > 0) {
        // Scrolling up
     if(check_ScrollingUp)
       {
          YourView.startAnimation(AnimationUtils.loadAnimation(context,R.anim.trans_downwards));
   check_ScrollingUp = false;
       }

    } else {
        // User scrolls down
         if(!check_ScrollingUp )
             {
                      YourView
                      .startAnimation(AnimationUtils
                      .loadAnimation(context,R.anim.trans_upwards));
   check_ScrollingUp = true;

               }
    }
 }

@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
    super.onScrollStateChanged(recyclerView, newState);

  }
});
Kaiser
  • 606
  • 8
  • 22
Santanu Sur
  • 10,997
  • 7
  • 33
  • 52