Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide/show view when scrolling up/down?

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!

like image 422
f982 Avatar asked Jan 02 '23 18:01

f982


1 Answers

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);

  }
});
like image 126
Santanu Sur Avatar answered Jan 05 '23 16:01

Santanu Sur