显示/隐藏系统栏上的Android屏幕闪烁 [英] Android Screen Blinking on show/hide System Bars
问题描述
我想在用户向上滚动RecyclerView时显示系统栏,而在用户向下滚动时隐藏系统栏.但是,通过我的方法,它可以工作,但是在显示/隐藏过程中,内容会突然移动并闪烁.您在此处上传了有关该行为的视频: https://drive.google.com/open?id = 0B_b9EpRt-zyHVW54dkQzcXdUTXM
I want to show the System Bars when the user scrolls an RecyclerView upwards and hide the System Bars when the user scrolls down. However with my Approach it works, but the content strangly moves and blinks during the show/hide process. U uploaded a Video for the behaviour here: https://drive.google.com/open?id=0B_b9EpRt-zyHVW54dkQzcXdUTXM
基本上我有这两种方法:
Basically I have this two methods:
private void hideSystemUI() {
getActivity().getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar
| View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
| View.SYSTEM_UI_FLAG_IMMERSIVE);
}
private void showSystemUI() {
getActivity().getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
}
然后我将此onScrollListener设置为RecyclerView,以根据滚动来隐藏和显示状态栏:
And i set this onScrollListener to the RecyclerView to hide and show the Status bars depending on the scroll:
mFragmentListBinding.fragListRvMovies.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if (dy > 5) {
// hideSystemUI();
// SystemUIHelper.hide(getActivity());
hideSystemUI();
} else if (dy < -5) {
showSystemUI();
// showSystemUI();
// SystemUIHelper.show(getActivity());
}
if (pageToDownload < TOTAL_PAGES && layoutManager.findLastVisibleItemPosition() == adapter.movieList.size() - 1 && !isLoadingLocked && !isLoading) {
downloadMoviesList();
}
}
});
这是布局:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<android.support.v4.widget.DrawerLayout
android:id="@+id/frag_drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.CoordinatorLayout
android:id="@+id/frag_drawer_coordinator"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="false">
<android.support.design.widget.AppBarLayout
android:id="@+id/frag_drawer_appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay"
android:transitionGroup="false"
tools:targetApi="lollipop">
<android.support.v7.widget.Toolbar
android:id="@+id/frag_drawer_tb"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />
</android.support.design.widget.AppBarLayout>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/frag_drawer_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="false"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/frag_drawer_fab_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_margin="@dimen/material_component_floating_action_button_margin"
android:src="@drawable/ic_add_white_24dp"
android:visibility="gone"
android:fitsSystemWindows="false"
app:backgroundTint="@color/accent"
app:elevation="@dimen/elevation_fab_resting"
app:fabSize="normal" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/frag_drawer_fab_filter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_margin="@dimen/material_component_floating_action_button_margin"
android:fitsSystemWindows="false"
android:src="@drawable/ic_filter"
android:visibility="visible"
app:backgroundTint="@color/accent"
app:elevation="@dimen/elevation_fab_resting"
app:fabSize="normal" />
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.NavigationView
android:id="@+id/frag_drawer_nav"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:elevation="@dimen/elevation_nav_drawer"
app:headerLayout="@layout/layout_nv_header"
app:itemIconTint="@color/nav_icon_tint"
app:itemTextColor="@color/nav_text"
app:menu="@menu/menu_drawer" />
</android.support.v4.widget.DrawerLayout>
</layout>
我的问题
如何更流畅地显示和隐藏状态栏和导航视图?
My Question
How can I show and hide the Status bar and the Navigation View more smoothly ?
推荐答案
您看到的是 Layout
调整大小.防止这种情况的唯一方法是将 Layout
分层放置在系统视图下,因此不受影响.检出以下标志,这可能有助于防止调整大小:
What you are seeing is the Layout
resizing. The only way to prevent that is having the Layout
layered under the system view, so it isn't affected.
Checkout the following flags, which might help you prevent the resizing:
FLAG_LAYOUT_IN_SCREEN
FLAG_LAYOUT_INSET_DECOR
但是,请记住,内容的顶部将位于系统UI的后面,您需要对此进行补偿.
However, keep in mind that in the top part of your content will be behind the System UI and you need to offset that.
这篇关于显示/隐藏系统栏上的Android屏幕闪烁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!