Toolbar expands on hit

In my application, my toolbar collapses. If I run a specific fragment, I want to minimize the toolbar so that it behaves as “normal”, and the user cannot spend it on their own. Here is my layout that I use for my toolbar:

<android.support.design.widget.AppBarLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/appBarLayout" android:layout_width="match_parent" android:layout_height="wrap_content" > <android.support.design.widget.CollapsingToolbarLayout android:id="@+id/collapsing_toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" app:contentScrim="?attr/colorPrimary" app:expandedTitleMarginEnd="64dp" app:expandedTitleMarginStart="48dp" app:titleEnabled="false" app:layout_scrollFlags="scroll|exitUntilCollapsed"> <ImageView android:src="@drawable/drawer_background" app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed" android:layout_width="match_parent" android:layout_height="172dp" android:scaleType="centerCrop" app:layout_collapseMode="parallax" android:minHeight="100dp" /> <android.support.v7.widget.Toolbar android:id="@+id/toolBar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" app:layout_collapseMode="pin" app:layout_scrollFlags="scroll|enterAlways" /> </android.support.design.widget.CollapsingToolbarLayout> </android.support.design.widget.AppBarLayout> 

I can also collapse the layout from the code as follows:

 appBarLayout.setExpanded(false, false); final AppBarLayout.LayoutParams params = (AppBarLayout.LayoutParams) collapsingToolbarLayout.getLayoutParams(); params.setScrollFlags(0); collapsingToolbarLayout.setLayoutParams(params); 

But that does not help. If the user moves from the toolbar, he will expand.

Do you have any ideas?

+11
android android-toolbar android-appbarlayout
Mar 06 '16 at 0:10
source share
6 answers

The main problem is that there is still no CollapsingToolbarLayout.lock(); method CollapsingToolbarLayout.lock(); (v23.2.1 design support). We hope that this feature will be included in a future version. Until then, I have been proposing the following workaround:

You can collapse and lock the toolbar:

 appbar.setExpanded(false,false); CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams)appbar.getLayoutParams(); lp.height = (int) getResources().getDimension(R.dimen.toolbar_height); 

and open it:

  CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams)appbar.getLayoutParams(); lp.height = (int) getResources().getDimension(R.dimen.nav_header_height); 

However, there is a problem with the above code:

when the Coordinator is forced to compress. If the title of our toolbar has not disappeared, but CollapsingToolbarLayout.setTitle(CharSequence title); does not work any more. To fix this, we add a TextView to the toolbar and accordingly manage its visibility. (we want it to “go” to the fragment that has the toolbar “unlocked” and “visible” in the fragment that has its toolbar “locked”.

We need to set the TextView android:textAppearance same way as the CollapsingToolbarLayout app:collapsedTitleTextAppearance , in order to be consistent and not disturb the user with different sizes and colors of the toolbar text.

So, with this interface:

 public interface ToolbarManipulation { void collapseToolbar(); void expandToolbar(); void setTitle(String s); } 

such as:

 @Override public void collapseToolbar(){ appbar.setExpanded(false,false); CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams)appbar.getLayoutParams(); lp.height = (int) getResources().getDimension(R.dimen.toolbar_height); toolbarCollapsedTitle.setVisibility(View.VISIBLE); } @Override public void expandToolbar() { CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams)appbar.getLayoutParams(); lp.height = (int) getResources().getDimension(R.dimen.nav_header_height); toolbarCollapsedTitle.setVisibility(View.GONE); } @Override public void setTitle(String s) { collapsingToolbarLayout.setTitle(s); toolbarCollapsedTitle.setText(s); collapsingToolbarLayout.invalidate(); toolbarCollapsedTitle.invalidate(); } 

and main xml:

 .... <android.support.design.widget.CollapsingToolbarLayout android:id="@+id/collapsing_toolbar" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" app:contentScrim="?attr/colorPrimary" app:layout_scrollFlags="scroll|exitUntilCollapsed" app:collapsedTitleTextAppearance="@style/CollapsedAppBar" > <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:layout_collapseMode="pin" app:popupTheme="@style/AppTheme.PopupOverlay" > <TextView android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:visibility="gone" android:layout_gravity="center_vertical|start" android:gravity="center_vertical|start" android:id="@+id/toolbar_collapsed_title" android:textAppearance="@style/CollapsedAppBar" /> </android.support.v7.widget.Toolbar> </android.support.design.widget.CollapsingToolbarLayout> ...... 

You can lock and unlock the toolbar as you wish. Your fragments should call these functions in onResume ();

I downloaded an example implementation here .

Note. This is just a workaround, and obviously not the cleanest solution. We are expecting a newer version of com.android.support to solve this problem.

+8
Mar 15 '16 at 17:42
source share

I'm not quite sure what this is what you are looking for

 CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) appBarLayout.getLayoutParams(); AppBarLayout.Behavior behavior = (AppBarLayout.Behavior) params.getBehavior(); behavior.setDragCallback(new AppBarLayout.Behavior.DragCallback() { @Override public boolean canDrag(@NonNull AppBarLayout appBarLayout) { return false; } }); 
+9
Mar 09 '16 at 19:49
source share

Try changing the height of the AppBar. This works for me.

 public void lockAppBar() { int appbarHeight = (int)getResources().getDimension(R.dimen.your_fixed_appbar_height); getView().findViewById(R.id.my_appbar).getLayoutParams().height = appbarHeight; } 
+2
Mar 11 '16 at 20:31
source share

Do findViewById(R.id.image_view).setVisibility(View.GONE); where image_view is the imageView identifier in the toolbar . . But if you want to do this for a specific fragment, I suggest calling the same thing using an exchange of activity fragmentation.

+2
Mar 14 '16 at 19:10
source share

None of the solutions provided worked for me, except this one. With this solution, I can easily control the state of the minimized toolbar. This will prevent the expansion / inclusion of the minimization of the collapsible toolbar and the setting of a name for it.

 public void lockAppBar(boolean locked,String title) { if(locked){ appBarLayout.setExpanded(false, true); int px = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 80, getResources().getDisplayMetrics()); CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams)appBarLayout.getLayoutParams(); lp.height = px; appBarLayout.setLayoutParams(lp); collapsingToolbarLayout.setTitleEnabled(false); toolbar.setTitle(title); }else{ appBarLayout.setExpanded(true, false); appBarLayout.setActivated(true); CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams) appBarLayout.getLayoutParams(); lp.height = (int) getResources().getDimension(R.dimen.toolbarExpandHeight); collapsingToolbarLayout.setTitleEnabled(true); collapsingToolbarLayout.setTitle(title); } } 
+2
Jun 29 '16 at 5:15
source share

Well try setting some parameters in toolBar, tabLayout programmatically, I have ViewPager changing flags onPageSelected (int)

  @Override public void onPageSelected(int position) { AppBarLayout.LayoutParams params = new AppBarLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); if (position == 3) {//listen for scrolls only for Fragment 3 params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL | AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS); toolBar.setLayoutParams(params);//hide as per scroll params = new AppBarLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS); tabLayout.setLayoutParams(params);//always visible } else { appBarLayout.setExpanded(false, true); params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS | AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS_COLLAPSED); toolBar.setLayoutParams(params);//don't listen to scrolls tabLayout.setLayoutParams(params);//dont listen to scrolls } } 

Use / Set Flags Higher Than You Need. AppBarLayout.LayoutParams

Happy_Coding;

+1
Mar 14 '16 at 13:39
source share



All Articles