本文介绍了滚动列表视图时,Android ActionBar 隐藏/显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在用户向上/向下滚动列表视图时隐藏/显示兼容性操作栏.当操作栏消失时,列表也将占据操作栏的空间.

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.

我采用了一种方法,但它会产生难看的闪烁.有没有人对这个问题有适当的解决方案?所需的行为类似于 Google+ 应用中的隐藏/显示机制(我对页脚不感兴趣).

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).

我尝试过的内容如下:

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;
            }
        });
    }

推荐答案

为了避免重新调整布局导致的闪烁,您可以使用操作栏中的叠加属性.

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

但是这个解决方案会导致操作栏覆盖你的listview,所以你需要添加一个这样大小的标题:

But this solution causes that the action bar overlays your listview, so you need to add a header with this size:

"?android:attr/actionBarSize"

例如:

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.

这篇关于滚动列表视图时,Android ActionBar 隐藏/显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!