向上/向下滚动时如何隐藏/显示视图?

f98*_*982 3 java layout android scrollview

向上/向下滚动AndroidFoodpanda应用程序时如何隐藏/显示视图

在此处输入图片说明

我想在ScrollView向上/向下时隐藏/显示视图(线性或相对布局),就像上面的 gif 一样。

但是我的应用程序我不使用 Recyclerview 或列表视图(只是 textview)。

我怎样才能创建它?

谢谢!

San*_*Sur 7

将滚动侦听器添加到 RecylerView

  1. 如果用户正在向下滚动 - 然后开始翻译动画 UPWARDS

  2. 如果用户向上滚动 - 然后开始向下滚动动画

向上动画翻译:- (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>
Run Code Online (Sandbox Code Playgroud)

向下动画翻译:- (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>
Run Code Online (Sandbox Code Playgroud)

将滚动侦听器添加到 recyclerView(并进行检查)

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

  }
});
Run Code Online (Sandbox Code Playgroud)