How to hide and show the Android ActionBar like in Google Inbox app?
Asked Answered
P

2

0

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 makes the action hides after the list have scrolled some pixels, and I want to make this scroll like the Inbox App by Google, that is the action bar is pulled by the Layout according to the user scroll the screen up or down, in other words, I want to show/hide the action bar at the same time the user scroll down/up.

Someone face a problema like this? Some idea?

Picker answered 27/2, 2015 at 14:34 Comment(0)
A
3

super simple:

  1. ditch the ListView. ListView is the past. Use RecyclerView instead.
  2. add a RecyclerView.OnScrollListener to it, to get pixel-by-pixel scroll.
  3. use a Toolbar on your activity layout. So you can control the position of it.
  4. call setTranslationY(val) on your Toolbar to "scroll" it with the RecyclerView.

a few links with the docs for the mentioned classes:

Antiphrasis answered 27/2, 2015 at 14:46 Comment(2)
Thank you for your answer. Really, I should already have migrated to the RecyclerView. I will try this approach and accept your answer as soon as I finish.Picker
All the fancy cool effects you saw on apps with lists lately (quick return, animated items, etc) is using RecyclerView. ListView is very limited, Google is abandoning it and we (developers) should abandon ASAP too. Good luck.Antiphrasis
C
0

I created a solution based answer of @Budius and this link

First add this class

public class HidingRecyclerViewScrollListener extends RecyclerView.OnScrollListener {

    private final int UP = 0;
    private final int DOWN = 1;
    private final int margin = 5;
    private View mView;
    RecyclerView.LayoutManager mLayoutManager;

    private float currentPoint = 0;
    private int lastDirection = 1;

    public HidingRecyclerViewScrollListener(View view, LinearLayoutManager layoutManager) {
        this.mLayoutManager = layoutManager;
        mView = view;
    }

    public HidingRecyclerViewScrollListener(View view, GridLayoutManager layoutManager) {
        this.mLayoutManager = layoutManager;
        mView = view;
    }

    public HidingRecyclerViewScrollListener(View view, StaggeredGridLayoutManager layoutManager) {
        this.mLayoutManager = layoutManager;
        mView = view;
    }

    public int getFirstVisibleItem(int[] firstVisibleItemPositions) {
        int minSize = 0;
        for (int i = 0; i < firstVisibleItemPositions.length; i++) {
            if (i == 0) {
                minSize = firstVisibleItemPositions[i];
            } else if (firstVisibleItemPositions[i] < minSize) {
                minSize = firstVisibleItemPositions[i];
            }
        }
        return minSize;
    }


    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);
        float nextPoint = currentPoint + dy;


        if (nextPoint < 0) {
            nextPoint = 0;
        }


        if (nextPoint > viewSize()) {
            nextPoint = viewSize();
        }

        lastDirection = nextPoint >= currentPoint ? UP : DOWN;

        currentPoint = nextPoint;

        mView.setTranslationY(-currentPoint);
    }

    @Override
    public void onScrollStateChanged(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);
        }
    }

    private int getFirstVisibleItem() {
        int firstVisibleItem = 0;
        if (mLayoutManager instanceof StaggeredGridLayoutManager) {
            int[] lastVisibleItemPositions = ((StaggeredGridLayoutManager) mLayoutManager).findFirstVisibleItemPositions(null);
            firstVisibleItem = getFirstVisibleItem(lastVisibleItemPositions);
        } else if (mLayoutManager instanceof LinearLayoutManager) {
            firstVisibleItem = ((LinearLayoutManager) mLayoutManager).findFirstVisibleItemPosition();
        } else if (mLayoutManager instanceof GridLayoutManager) {
            firstVisibleItem = ((GridLayoutManager) mLayoutManager).findFirstVisibleItemPosition();
        }
        return firstVisibleItem;
    }

    private int viewSize() {
        return mView.getHeight() + margin;
    }
}

Is simple to use, only add using addOnScrollListener of your recyclerView

recyclerView = (RecyclerView) view.findViewById(R.id.mRecyclerView );
layoutManager = new LinearLayoutManager(getContext());
recyclerView.setLayoutManager(layoutManager);
recyclerView.addOnScrollListener(new HidingRecyclerViewScrollListener(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);
Cymatium answered 24/5, 2016 at 13:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.