gpt4 book ai didi

android - 滚动时隐藏 Android ActionBar

转载 作者:行者123 更新时间:2023-11-29 20:53:50 24 4
gpt4 key购买 nike

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

这是我的 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>

这是我显示/隐藏操作栏的代码

@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。

enter image description here

如何在隐藏 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);

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

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

或者您可以在 XML 中将 padding 设置为 ListView,但这很糟糕,因为当 ActionBar隐藏。

如果你想给 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.

这一定对你有帮助。

关于android - 滚动时隐藏 Android ActionBar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28319859/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com