How To Hide And Show The Android Actionbar Like In Google Inbox App?
I wnat to show and hide the Android ActionBar when the user scrolls the screen. I found some examples, like this question in SO. But the code showed in this question and its answer
Solution 1:
super simple:
- ditch the
ListView
. ListView is the past. UseRecyclerView
instead. - add a
RecyclerView.OnScrollListener
to it, to get pixel-by-pixel scroll. - use a
Toolbar
on your activity layout. So you can control the position of it. - call
setTranslationY(val)
on yourToolbar
to "scroll" it with theRecyclerView
.
a few links with the docs for the mentioned classes:
- http://android-developers.blogspot.de/2014/10/appcompat-v21-material-design-for-pre.html
- https://developer.android.com/reference/android/support/v7/widget/RecyclerView.html
- https://developer.android.com/reference/android/support/v7/widget/RecyclerView.OnScrollListener.html
- http://developer.android.com/reference/android/widget/Toolbar.html
- https://developer.android.com/reference/android/view/View.html#setTranslationY(float)
Solution 2:
I created a solution based answer of @Budius and this link
First add this class
publicclassHidingRecyclerViewScrollListenerextendsRecyclerView.OnScrollListener {
privatefinalintUP=0;
privatefinalintDOWN=1;
privatefinalintmargin=5;
private View mView;
RecyclerView.LayoutManager mLayoutManager;
privatefloatcurrentPoint=0;
privateintlastDirection=1;
publicHidingRecyclerViewScrollListener(View view, LinearLayoutManager layoutManager) {
this.mLayoutManager = layoutManager;
mView = view;
}
publicHidingRecyclerViewScrollListener(View view, GridLayoutManager layoutManager) {
this.mLayoutManager = layoutManager;
mView = view;
}
publicHidingRecyclerViewScrollListener(View view, StaggeredGridLayoutManager layoutManager) {
this.mLayoutManager = layoutManager;
mView = view;
}
publicintgetFirstVisibleItem(int[] firstVisibleItemPositions) {
intminSize=0;
for (inti=0; i < firstVisibleItemPositions.length; i++) {
if (i == 0) {
minSize = firstVisibleItemPositions[i];
} elseif (firstVisibleItemPositions[i] < minSize) {
minSize = firstVisibleItemPositions[i];
}
}
return minSize;
}
@OverridepublicvoidonScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
floatnextPoint= currentPoint + dy;
if (nextPoint < 0) {
nextPoint = 0;
}
if (nextPoint > viewSize()) {
nextPoint = viewSize();
}
lastDirection = nextPoint >= currentPoint ? UP : DOWN;
currentPoint = nextPoint;
mView.setTranslationY(-currentPoint);
}
@OverridepublicvoidonScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if (newState == RecyclerView.SCROLL_STATE_IDLE) {
if (lastDirection == UP) {
if (getFirstVisibleItem() > 0) {
currentPoint = viewSize();
}
}
if (lastDirection == DOWN) {
//Volta para origem
currentPoint = 0;
}
mView.animate().translationY(-currentPoint);
}
}
privateintgetFirstVisibleItem() {
intfirstVisibleItem=0;
if (mLayoutManager instanceof StaggeredGridLayoutManager) {
int[] lastVisibleItemPositions = ((StaggeredGridLayoutManager) mLayoutManager).findFirstVisibleItemPositions(null);
firstVisibleItem = getFirstVisibleItem(lastVisibleItemPositions);
} elseif (mLayoutManager instanceof LinearLayoutManager) {
firstVisibleItem = ((LinearLayoutManager) mLayoutManager).findFirstVisibleItemPosition();
} elseif (mLayoutManager instanceof GridLayoutManager) {
firstVisibleItem = ((GridLayoutManager) mLayoutManager).findFirstVisibleItemPosition();
}
return firstVisibleItem;
}
privateintviewSize() {
return mView.getHeight() + margin;
}
}
Is simple to use, only add using addOnScrollListener of your recyclerView
recyclerView = (RecyclerView) view.findViewById(R.id.mRecyclerView );
layoutManager = newLinearLayoutManager(getContext());
recyclerView.setLayoutManager(layoutManager);
recyclerView.addOnScrollListener(newHidingRecyclerViewScrollListener(mView, layoutManager));
mView is the view you want to control
You can user this class with LinearLayoutManager, GridLayoutManager and StaggeredGridLayoutManager (but i never test with GridLayoutManager)
In RecyclerView use the code below to the list does not stay below the object you want to control, this code is simpler and better to test, but use a Hearder in RecyclerView as is said in the link above has a better result.
android:clipToPadding="false"android:paddingTop="@dimen/outher_view_dimem"
If you using SwipeRefreshLayout use code below to show ProgressBar
swipeRefreshLayout.setProgressViewOffset(true, 0, mViewHeight + actionBarHeight);
Post a Comment for "How To Hide And Show The Android Actionbar Like In Google Inbox App?"