Hide FAB in NestedScrollView when scrolling

后端 未结 4 1440
误落风尘
误落风尘 2021-02-07 14:06

I am having a nestedscrollview with content like some linearlayouts and textviews. I am using a floatingactionbutton library for some reasons, as well. So I can\'t use any behav

4条回答
  •  眼角桃花
    2021-02-07 14:09

    After spending such time i have found the solution for it. It may work in all situations. Though it is a hack not the proper solution but you can apply it to make this thing work.

    As we know setOnScrollChangeListener will only work if minimum api 23, so what if my minimum api level is less then 23.

    So I found out solution from stack overflow that we can use getViewTreeObserver().addOnScrollChangedListener for that so this will be compatible solution for all devices.

    Now let's move to the final solution of over problem "Hide fab button when nested scroll view scrolling and Show fab button when nested scroll view in ideal state"

    So for that we can use Handler with postDelayed to slove this issue.

    1. Define on variable in you context private int previousScrollY = 0;

    2. Then use getViewTreeObserver().addOnScrollChangedListener to your nested scroll view like this.

    NESTEDSCROLLVIEW.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() { @Override public void onScrollChanged() { new Handler().postDelayed(new Runnable() { @Override public void run() { if (NESTEDSCROLLVIEW.getScrollY() == previousScrollY) { FABBUTTON.setVisibility(View.VISIBLE); } else { FABBUTTON.setVisibility(View.INVISIBLE); } } }, 10); previousScrollY = NESTEDSCROLLVIEW.getScrollY(); } });

    1. Now you are ready to go....

提交回复
热议问题