I'm using appCompat action bar. I'm try to make a home feed like instagram while the user scroll down the action bar is hide and vice versa.
The code below is the most basic way to hide and show a action while scrolling the list view
int mLastFirstVisibleItem = 0;
if (view.getId() == mListView.getId()) {
final int currentFirstVisibleItem = mListView.getFirstVisiblePosition();
if (currentFirstVisibleItem > mLastFirstVisibleItem) {
getSupportActionBar().hide();
} else if (currentFirstVisibleItem < mLastFirstVisibleItem) {
getSupportActionBar().show();
}
mLastFirstVisibleItem = currentFirstVisibleItem;
}
The problem is while I scroll the listview slowly the action bar will keep hide and show repeatly.It does not give me the ability to control how much I want hide or show the action bar. What I want is when I scroll down or up the list view the action bar will follow the gesture eventually when the user hand off from the screen it will hide or show depend on the size of remain visible action bar on the screen.
For instance while I scrolling down 2px the action bar will going up or hide 2px. And if the action bar size remain on the screen is 20px while the action bar size is 50px it will hide it like the code below.
//this is just a demo of how I want the action bar to work not any type of programming language
//visibleActionBarOnscreen is the actionbarsize remain on screen
if(visibleActionBarOnScreen > actiobarSize/2){
//show the action bar
}else{
//hide the action bar
}
Push out/pull in ActionBar when ListView is scrolled this link teaches how control the action bar. But I still dont know how to make it done.In additional it doesn't auto readjust the action bar to either fully hide or show but let the action bar appear half on screen while the user hand off from the screen. Please help me done this I will be 100% apperciate!
If you want a smooth-transitioning bar, you might want to implement your own custom actionbar using a Layout (probably Horizontal Linear Layout, to put stuff in the actionbar horizontally) and animating/transitioning your app-header by setting negative margins to pull it off the top of the screen. (This is an alternative solution with more customizability.)
This is an example of animating layout margins: Change Left Margin Using Animation (You'll have to change it to top margin)
However, that won't be a "sticking" action-bar like you're saying (it won't stay at 2px or half way etc). To implement a "sticking" scrolling actionbar you'll want to override the OnScrollListener()
on your ListView or CustomScrollView and do some math like this:
int scrollpos = (int)view.getScrollY();
if(scrollpos<70) { //trigger scroll movement
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)findViewById(R.id.ListView).getLayoutParams();
params.topMargin = -scrollpos;
//if scrollpos = 0, then topMargin will = 0, completely visible.
//if scroll is past 70, then topMargin will = -70, completely hidden.
//assuming your action-bar is 70px
findViewById(R.id.ListView).setLayoutParams(params);
}
You'll actually have to set topMargin
by getting and setting LayoutParams
, I didn't include that up there. I'm away from any Java IDE, so I can't compile code at the moment.
Hope this helps,