Android ActionBar hide/show when scrolling list view
Asked Answered
E

4

14

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

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;
            }
        });
    }
Eec answered 26/8, 2014 at 7:32 Comment(1)
have you checked the Google IO code latest. It contains best practices github.com/google/ioschedEdieedification
P
5

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

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

"?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.

Pointdevice answered 2/9, 2014 at 11:0 Comment(1)
Varela Gomez I know its been a while but can you state the content of listview_header.xml?Minny
B
3

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/

Barneybarnhart answered 25/9, 2014 at 14:52 Comment(0)
K
2

you could try this library. Hope it helps https://github.com/ksoichiro/Android-ObservableScrollView

Kelle answered 24/3, 2015 at 13:32 Comment(0)
Y
0

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

Your answered 24/5, 2016 at 14:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.