工具栏留白 space 并在 RecyclerView 滚动时隐藏
Toolbar leaving white space with hide on RecyclerView scroll
我试图在 RecyclerView 滚动条上隐藏我的工具栏,到目前为止它似乎已经隐藏,但它留下了一个白色的空白。我很确定这与我的 MainActivity 布局和 FrameLayout 中的片段的叠加有关。
这是我的activity_main.xml
。我需要 FrameLayout,因为我通过在导航抽屉中选择一个项目来加载不同的片段。
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:elevation="7dp"
tools:context=".MainActivity"
android:fitsSystemWindows="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="@layout/toolbar" />
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF" />
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="@+id/navigation_view"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/header"
app:menu="@menu/drawer" />
</android.support.v4.widget.DrawerLayout>
下面是包含 RecyclerView 的片段中的代码。
...
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_main, container, false);
getActivity().setTitle("Unit One");
rv = (RecyclerView) v.findViewById(R.id.rv);
rv.setHasFixedSize(true);
llm = new LinearLayoutManager(getActivity());
rv.setLayoutManager(llm);
initializeData();
initializeAdapter();
rv.addOnScrollListener(showHideToolbarListener = new RecyclerViewUtils.ShowHideToolbarOnScrollingListener(MainActivity.toolbar));
if (savedInstanceState != null) {
showHideToolbarListener.onRestoreInstanceState((RecyclerViewUtils.ShowHideToolbarOnScrollingListener.State) savedInstanceState
.getParcelable(RecyclerViewUtils.ShowHideToolbarOnScrollingListener.SHOW_HIDE_TOOLBAR_LISTENER_STATE));
}
return v;
}
@Override
public void onSaveInstanceState(Bundle outState) {
outState.putParcelable(RecyclerViewUtils.ShowHideToolbarOnScrollingListener.SHOW_HIDE_TOOLBAR_LISTENER_STATE,
showHideToolbarListener.onSaveInstanceState());
super.onSaveInstanceState(outState);
}
最后,fragment_main.xml
中的 RecyclerView 布局:
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:orientation="vertical"
android:scrollbars="vertical"
android:id="@+id/rv" />
这是我的 other post, but I wanted to ask a new question since I'm trying a different method this time (don't read the entire post, see the edit at the bottom). Here is his MainActivity and the actual scrolling listener code 的跟进。
如果这听起来像是我说的 "here's my shit go fix it",我深表歉意,但我已经花了将近两个小时来寻找可能的解决方案。 一如既往,非常感谢您的帮助。
我认为您用来设置工具栏动画的 translateY 只会移动屏幕上绘制的内容,而不是视图位置本身。这意味着持有工具栏的视图仍然占据线性布局顶部的 space。
使用他在 main_activity.xml 中使用的相同布局。
您还可以使用 FrameLayout。重要的是,您将工具栏放在 RecyclerView 的顶部(在 xml 下方)并向 RecyclerView 添加等于工具栏高度的顶部填充。
<android.support.v7.widget.RecyclerView
android:clipToPadding="false"
android:paddingTop="?attr/actionBarSize" />
<android.support.v7.widget.Toolbar
..... />
最好的方法是使用新的 Design Support Library 及其 CoordinatorLayout
:
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_scrollFlags="scroll|enterAlways" />
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
有关更多示例,您可以从 Chris Banes 的演示应用程序中获得启发 CheeseSquare or by this tutorial。
我试图在 RecyclerView 滚动条上隐藏我的工具栏,到目前为止它似乎已经隐藏,但它留下了一个白色的空白。我很确定这与我的 MainActivity 布局和 FrameLayout 中的片段的叠加有关。
这是我的activity_main.xml
。我需要 FrameLayout,因为我通过在导航抽屉中选择一个项目来加载不同的片段。
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:elevation="7dp"
tools:context=".MainActivity"
android:fitsSystemWindows="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="@layout/toolbar" />
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF" />
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="@+id/navigation_view"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/header"
app:menu="@menu/drawer" />
</android.support.v4.widget.DrawerLayout>
下面是包含 RecyclerView 的片段中的代码。
...
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_main, container, false);
getActivity().setTitle("Unit One");
rv = (RecyclerView) v.findViewById(R.id.rv);
rv.setHasFixedSize(true);
llm = new LinearLayoutManager(getActivity());
rv.setLayoutManager(llm);
initializeData();
initializeAdapter();
rv.addOnScrollListener(showHideToolbarListener = new RecyclerViewUtils.ShowHideToolbarOnScrollingListener(MainActivity.toolbar));
if (savedInstanceState != null) {
showHideToolbarListener.onRestoreInstanceState((RecyclerViewUtils.ShowHideToolbarOnScrollingListener.State) savedInstanceState
.getParcelable(RecyclerViewUtils.ShowHideToolbarOnScrollingListener.SHOW_HIDE_TOOLBAR_LISTENER_STATE));
}
return v;
}
@Override
public void onSaveInstanceState(Bundle outState) {
outState.putParcelable(RecyclerViewUtils.ShowHideToolbarOnScrollingListener.SHOW_HIDE_TOOLBAR_LISTENER_STATE,
showHideToolbarListener.onSaveInstanceState());
super.onSaveInstanceState(outState);
}
最后,fragment_main.xml
中的 RecyclerView 布局:
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:orientation="vertical"
android:scrollbars="vertical"
android:id="@+id/rv" />
这是我的 other post, but I wanted to ask a new question since I'm trying a different method this time (don't read the entire post, see the edit at the bottom). Here is his MainActivity and the actual scrolling listener code 的跟进。
如果这听起来像是我说的 "here's my shit go fix it",我深表歉意,但我已经花了将近两个小时来寻找可能的解决方案。 一如既往,非常感谢您的帮助。
我认为您用来设置工具栏动画的 translateY 只会移动屏幕上绘制的内容,而不是视图位置本身。这意味着持有工具栏的视图仍然占据线性布局顶部的 space。
使用他在 main_activity.xml 中使用的相同布局。 您还可以使用 FrameLayout。重要的是,您将工具栏放在 RecyclerView 的顶部(在 xml 下方)并向 RecyclerView 添加等于工具栏高度的顶部填充。
<android.support.v7.widget.RecyclerView
android:clipToPadding="false"
android:paddingTop="?attr/actionBarSize" />
<android.support.v7.widget.Toolbar
..... />
最好的方法是使用新的 Design Support Library 及其 CoordinatorLayout
:
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_scrollFlags="scroll|enterAlways" />
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
有关更多示例,您可以从 Chris Banes 的演示应用程序中获得启发 CheeseSquare or by this tutorial。