Toolbar leaving blank space hiding in RecyclerView scroll

I'm trying to hide my toolbar in a RecyclerView scroll, and so far it seems to be hidden, but leaves it blank. I'm sure this has something to do with overlaying my MainActivity layout and fragment in FrameLayout.

why is this happening to me ._.

Here is my activity_main.xml . I need FrameLayout, because I load different fragments with an item selection 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 snippet 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 finally, 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 continuation of my other post , but I wanted to ask a new question, since I'm trying a different method this time (do not read the whole post, see editing below). Here is its MainActivity and the actual scroll listener code .

I apologize if it seems to me that I say, β€œHere is my shit, correct it,” but I spent almost two hours looking for a possible solution to this. As always, I am very grateful for the help.

+3
android android-fragments android-recyclerview toolbar
source share
2 answers

I think that the translateY that you use to animate the toolbar only shifts what is drawn on the screen, not the viewpoint itself. This means that the view holding the toolbar still occupies this space at the top in the linear layout.

Use the same layout that it uses in main_activity.xml . You can also use FrameLayout. The important thing is that you place the toolbar on top of the RecyclerView (below in xml) and add a top add-on to the RecyclerView equal to the height of the toolbar.

 <android.support.v7.widget.RecyclerView android:clipToPadding="false" android:paddingTop="?attr/actionBarSize" /> <android.support.v7.widget.Toolbar ..... /> 
+5
source share

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

Moreover, you can get inspiration from the Chris Banes CheeseSquare demo app or this tutorial .

+2
source share

All Articles