滚动时隐藏 Android ActionBar

Hiding Android ActionBar when scrolling

我想在我的 RecyclerView 上滚动时隐藏 ActionBar。为此,我使用了快速 return 模式。

这是我的 styles.xml

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->

    <item name="android:windowActionBarOverlay">true</item>
    <!-- Support library compatibility -->
    <item name="windowActionBarOverlay">true</item>
</style>

这是我的布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="?attr/actionBarSize">

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipe_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <include layout="@layout/recyclerview"/>

    </android.support.v4.widget.SwipeRefreshLayout>

    <include layout="@layout/empty_layout"/>

    <fragment
        android:id="@+id/adFragment"
        android:name="com.myapp.fragment.AdFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" />

</RelativeLayout>

这是我的 show/hide 操作栏

代码
@Override
public void onUpOrCancelMotionEvent(ScrollState scrollState) {
    ActionBar ab = ((ActionBarActivity)getActivity()).getSupportActionBar();
    boolean tabletSize = getResources().getBoolean(R.bool.isTablet);
    if (!tabletSize) {
        if (scrollState == ScrollState.UP) {
            if (ab.isShowing()) {
                ab.hide();
            }
        } else if (scrollState == ScrollState.DOWN) {
            if (!ab.isShowing()) {
                ab.show();
            }
        }
    }
}

我正在使用 ?attr/actionBarSize 来避免闪烁。 但是当 ActionBar 隐藏时我仍然有 paddingTop。

当ActionBar被隐藏时,如何删除右侧屏幕上的这个paddingTop?

只需在您的 ListView:

中使用此侦听器
AbsListView.OnScrollListener onScrollListener = new AbsListView.OnScrollListener() {

        int mLastFirstVisibleItem = 0;

        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
        }

        @Override
        public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount,
                             int totalItemCount) {


            final int lastItem = firstVisibleItem + visibleItemCount;

            if (absListView.getId() == wallListView.getId()) {

                final int currentFirstVisibleItem = wallListView.getFirstVisiblePosition();
                if (currentFirstVisibleItem > mLastFirstVisibleItem) {
                    actionBar.hide();
                } else if (currentFirstVisibleItem < mLastFirstVisibleItem) {
                    actionBar.show();
                }

                mLastFirstVisibleItem = currentFirstVisibleItem;
            }
        }

    };

如果你想要顶部的黑色区域,你必须:

将此代码添加到您的 Activity

requestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY); //ADD THIS BEFORE setContentView(...);
setContentView(R.layout.your_layout);

现在就好了,但是你还需要设置 space 的大小等于 ActionBar 的大小。我们将 Header 添加到您的 ListView :

        View padding = new View(getApplicationContext());
        padding.setBackgroundColor(Color.TRANSPARENT);
        padding.setHeight(action_bar_size);
        your_list_view.addHeaderView(padding);

或者您可以在 XML 中将填充设置为 ListView,但这很糟糕,因为当 ActionBar 隐藏时 space 将被剪切。

如果你想给SwipeRefreshLayout添加偏移,尝试使用:

public void setProgressViewOffset (boolean scale, int start, int end)

The refresh indicator starting and resting position is always positioned near the top of the refreshing content. This position is a consistent location, but can be adjusted in either direction based on whether or not there is a toolbar or actionbar present.

Parameters scale Set to true if there is no view at a higher z-order than where the progress spinner is set to appear. start The offset in pixels from the top of this view at which the progress spinner should appear. end The offset in pixels from the top of this view at which the progress spinner should come to rest after a successful swipe gesture.

这一定对你有帮助。