-3

I am using this library to create a floating action button in my android application. What I need is to hide the floating action button when I scroll down, and show it again when I scroll up. The problem is I have a FrameLayout and a ScrollView that doesn't contain a setOnScrollListener()!

I read this solution, but supposedly it is laggy.

Can anyone tell me how to reach get the functionality I'm looking for without losing performance?

Community
  • 1
  • 1
Mohamad MohamadPoor
  • 1,350
  • 2
  • 14
  • 35

2 Answers2

1

Try this library.

It provides a floating action button that disappears when you scroll down :-)

Alex Crist
  • 1,059
  • 2
  • 12
  • 22
1

One option is to create an ObservableScrollView that has a scroll listener. Google uses this approach in the IOSched14 app. One way of creating this might be:

public class ObservableScrollView extends ScrollView {
    private boolean mScrollingEnabled = true;
    private ArrayList<Callbacks> mCallbacks = new ArrayList<Callbacks>();

    public ObservableScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        for (Callbacks c : mCallbacks) {
            c.onScrollChanged(l - oldl, t - oldt);
        }
    }

    @Override
    public int computeVerticalScrollRange() {
        return super.computeVerticalScrollRange();
    }

    public void addCallbacks(Callbacks listener) {
        if (!mCallbacks.contains(listener)) {
            mCallbacks.add(listener);
        }
    }

    public static interface Callbacks {
        public void onScrollChanged(int deltaX, int deltaY);
    }



    public void setScrollingEnabled(boolean scrollingEnabled) {
        mScrollingEnabled = scrollingEnabled;
    }

//    @Override
//    public boolean onTouchEvent(MotionEvent ev) {
//        if (!mScrollingEnabled) return false;
//        return super.onTouchEvent(ev);
//    }

}

Simply add this instead of your ScrollView and then attach a listener using the addCallbacks method:

ObservableScrollView scrollView = new ObservableScrollView(context);
scrollView.addCallbacks(this);
Chris
  • 1,180
  • 1
  • 9
  • 17