/** * Odoo, Open Source Management Solution * Copyright (C) 2012-today Odoo SA (<http:www.odoo.com>) * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as * published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see <http:www.gnu.org/licenses/> * * Created on 9/1/15 2:24 PM */ package odoo.controls.fab; import android.view.View; import android.widget.AbsListView; public class DirectionScrollListener implements AbsListView.OnScrollListener { public static final String TAG = DirectionScrollListener.class.getSimpleName(); private static final int DIRECTION_CHANGE_THRESHOLD = 1; private final FloatingActionButton mFloatingActionButton; private int mPrevPosition; private int mPrevTop; private boolean mUpdated; DirectionScrollListener(FloatingActionButton floatingActionButton) { mFloatingActionButton = floatingActionButton; } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { final View topChild = view.getChildAt(0); int firstViewTop = 0; if (topChild != null) { firstViewTop = topChild.getTop(); } boolean goingDown; boolean changed = true; if (mPrevPosition == firstVisibleItem) { final int topDelta = mPrevTop - firstViewTop; goingDown = firstViewTop < mPrevTop; changed = Math.abs(topDelta) > DIRECTION_CHANGE_THRESHOLD; } else { goingDown = firstVisibleItem > mPrevPosition; } if (changed && mUpdated) { mFloatingActionButton.hide(goingDown); } mPrevPosition = firstVisibleItem; mPrevTop = firstViewTop; mUpdated = true; } @Override public void onScrollStateChanged(AbsListView view, int scrollState) { } }