滚动列表视图时,Android ActionBar 隐藏/显示
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25500353/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android ActionBar hide/show when scrolling list view
提问by dorin
I want to hide/show the compatibility action bar as the user scrolls up/down a listview. When the action bar disappears the list will occupy also the action bar's space.
我想在用户向上/向下滚动列表视图时隐藏/显示兼容性操作栏。当操作栏消失时,列表也将占据操作栏的空间。
I've followed an approach but it generates an ugly flickering. Does anyone have a proper solution for this problem? The desired behaviour is like the hide/show mechanism in the Google+ app (I'm not interested about the footer).
我遵循了一种方法,但它会产生难看的闪烁。有没有人对这个问题有适当的解决方案?所需的行为就像 Google+ 应用中的隐藏/显示机制(我对页脚不感兴趣)。
What I've tried is presented below:
我尝试过的内容如下:
boolean tabletSize = getResources().getBoolean(R.bool.isTablet);
if (!tabletSize) {
final ActionBar actionBar = ((ActionBarActivity) getActivity()).getSupportActionBar();
lvNews.setOnScrollListener(new AbsListView.OnScrollListener() {
private int mLastFirstVisibleItem;
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
if (mLastFirstVisibleItem < firstVisibleItem) {
if (actionBar.isShowing()) {
actionBar.hide();
}
}
if (mLastFirstVisibleItem > firstVisibleItem) {
if (!actionBar.isShowing()) {
actionBar.show();
}
}
mLastFirstVisibleItem = firstVisibleItem;
}
});
}
采纳答案by Eduardo Varela Gómez
To avoid the flickering caused by the readjustment of the layout you can use the overlaying property in the action bar.
为了避免重新调整布局导致的闪烁,您可以使用操作栏中的叠加属性。
https://developer.android.com/training/basics/actionbar/overlaying.html
https://developer.android.com/training/basics/actionbar/overlaying.html
But this solution causes that the action bar overlays your listview, so you need to add a header with this size:
但是此解决方案会导致操作栏覆盖您的列表视图,因此您需要添加具有此大小的标题:
"?android:attr/actionBarSize"
"?android:attr/actionBarSize"
for example :
例如 :
mVideosListView = (ListView) getActivity().findViewById(android.R.id.list);
mVideosListView.addHeaderView(getActivity().getLayoutInflater().inflate(R.layout.listview_header,null));
Be careful with other elements like navigation drawer which is also affected by this.
小心其他元素,如导航抽屉也受此影响。
回答by Muatik
You need to make the action bar overlays the listview. By doing this you have the responsibility to manage of hiding and showing the action bar when necessary.
您需要使操作栏覆盖列表视图。通过这样做,您有责任在必要时管理隐藏和显示操作栏。
There is an example at the following page: http://www.techrepublic.com/article/pro-tip-maximize-android-screen-real-estate-by-showing-and-hiding-the-action-bar/
以下页面有一个例子:http: //www.techrepublic.com/article/pro-tip-maximize-android-screen-real-estate-by-showing-and-hiding-the-action-bar/
回答by TibiG
you could try this library. Hope it helps https://github.com/ksoichiro/Android-ObservableScrollView
你可以试试这个库。希望它有帮助https://github.com/ksoichiro/Android-ObservableScrollView
回答by Luciano Marqueto
See my answer on that link: How to hide and show the Android ActionBar like in Google Inbox app?I am using RecyclerView but the idea is the same
请参阅我在该链接上的回答:How to hide and show the Android ActionBar like in Google Inbox app? 我正在使用 RecyclerView 但想法是一样的