How to properly hide&show the actionbar using the new design library API?

后端 未结 3 1109
北海茫月
北海茫月 2020-12-28 15:10

Background

Suppose I have these elements in the activity:

  • actionbar (actually a Toolbar)
  • tabs (using TabLayout )
  • ViewPager, with fr
3条回答
  •  抹茶落季
    2020-12-28 15:56

    this is a nice workaround, but It's still very different than what the Play Store shows:

    for each fragment, call something like that:

        recyclerView.addOnScrollListener(new OnScrollListener() {
            @Override
            public void onScrollStateChanged(final RecyclerView recyclerView, final int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                ((MainActivity) getActivity()).onScrollStateChanged(recyclerView, newState);
            }
    
            @Override
            public void onScrolled(final RecyclerView recyclerView, final int dx, final int dy) {
                super.onScrolled(recyclerView, dx, dy);
                ((MainActivity) getActivity()).onScrolled(recyclerView, dx,dy);
            }
        });
    

    for the hosting activity:

    public void onScrollStateChanged(final RecyclerView recyclerView, final int newState) {
        switch (newState) {
            case RecyclerView.SCROLL_STATE_IDLE:
                mAppBarLayout.setExpanded(mLastDy <= 0, true);
                mLastDy = 0;
                break;
        }
    }
    
    public void onScrolled(final RecyclerView recyclerView, final int dx, final int dy) {
        mLastDy = dy == 0 ? mLastDy : dy;
    }
    

    Still, if anyone knows how to make it work well (using either ListView or RecyclerView for the fragments), just like on the Play-Store and other apps, please write it down.

提交回复
热议问题