0

我想在用户滚动屏幕时显示和隐藏 Android ActionBar。我找到了一些例子,比如SO 中的这个问题。但是这个问题中显示的代码及其答案在列表滚动一些像素后使动作隐藏,我想让这个滚动就像谷歌的收件箱应用程序一样,即动作栏是由布局根据用户拉动的向上或向下滚动屏幕,换句话说,我想在用户向下/向上滚动的同时显示/隐藏操作栏。

有人面临这样的问题吗?有什么想法?

4

2 回答 2

3

超级简单:

  1. 抛弃ListView. ListView 已成为过去。改为使用RecyclerView
  2. 添加一个RecyclerView.OnScrollListener,以获得逐像素滚动。
  3. Toolbar在您的活动布局上使用 a 。所以你可以控制它的位置。
  4. 呼吁setTranslationY(val)ToolbarRecyclerView.

与上述类的文档的一些链接:

于 2015-02-27T14:46:48.537 回答
0

我创建了一个基于解决方案的@Budius 答案和这个链接

首先添加这个类

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

使用简单,只需使用你的 recyclerView 的 addOnScrollListener 添加

recyclerView = (RecyclerView) view.findViewById(R.id.mRecyclerView );
layoutManager = new LinearLayoutManager(getContext());
recyclerView.setLayoutManager(layoutManager);
recyclerView.addOnScrollListener(new HidingRecyclerViewScrollListener(mView, layoutManager));
  • mView 是你要控制的视图

  • 您可以使用 LinearLayoutManager、GridLayoutManager 和 StaggeredGridLayoutManager 使用此类(但我从不使用 GridLayoutManager 进行测试)

在 RecyclerView 中使用下面的代码列表不会停留在您要控制的对象下方,此代码更简单,更好地测试,但在 RecyclerView 中使用 Hearder,如上面链接中所述,效果更好。

android:clipToPadding="false"
android:paddingTop="@dimen/outher_view_dimem"

如果您使用 SwipeRefreshLayout 使用下面的代码来显示 ProgressBar

swipeRefreshLayout.setProgressViewOffset(true, 0, mViewHeight + actionBarHeight);
于 2016-05-24T13:48:44.620 回答