Toolbar leaving white space with hide on RecyclerV

2020-03-04 03:16发布

问题:

I'm trying to hide my Toolbar on the RecyclerView scroll and so far it seems to have hidden, but it's leaving a white blank with it. I'm pretty sure this has something to do with the overlay of my MainActivity's layout and the fragment in the FrameLayout.

Here is my activity_main.xml. I need the FrameLayout because I am loading different fragments with selection of an item in the Navigation Drawer.

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

And here is the code in the fragment that contains the 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);
    }

And lastly, the RecyclerView layout in fragment_main.xml:

 <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" />

This is a follow up to my 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.

I apologize if this seems like me saying "here's my shit go fix it" but I've spent almost two hours searching for a possible solution to this. As always, I appreciate the help greatly.

回答1:

I think that translateY, which you're using to animate the toolbar, only shifts what's drawn on the screen and not the view location itself. That means that the view holding the toolbar still occupies that space at the top in the linear layout.

Use the same layout he's using in his main_activity.xml. You can also use a FrameLayout. The important thing is that you put the toolbar on top of the RecyclerView (below in the xml) and add a top padding to the RecyclerView equal to the toolbar height.

<android.support.v7.widget.RecyclerView
  android:clipToPadding="false"
  android:paddingTop="?attr/actionBarSize" />

<android.support.v7.widget.Toolbar
  ..... />


回答2:

The best approach is to use the new Design Support Library and its 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" />

For more example, you can get inspired by Chris Banes' demo application CheeseSquare or by this tutorial.