Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Toolbar hiding doesn't work

When I scroll ListView in Fragment my ToolBar doesn't hiding/showing. I used sample from here This my xml:

<?xml version="1.0" encoding="utf-8"?>
<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:id="@+id/home_appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <include
            layout="@layout/toolbar_layout"/>

        <android.support.design.widget.TabLayout
            android:id="@+id/home_tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/home_viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</android.support.design.widget.CoordinatorLayout>

And this my code in Fragment:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_home, container, false);

    mViewPager = (ViewPager) view.findViewById(R.id.home_viewpager);
    mAdapter = new HomeScreenPagerAdapter(getChildFragmentManager(), getActivity());
    mViewPager.setAdapter(mAdapter);
    mTabLayout = (TabLayout) view.findViewById(R.id.home_tabs);
    mTabLayout.setupWithViewPager(mViewPager);


    return view;
}

Add Toolbar layout

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    app:layout_scrollFlags="scroll|enterAlways|snap"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

I can run this code, but ToolBar doesn't hiding. How can I hide/show it?

like image 834
MyNameIs Avatar asked Feb 21 '26 18:02

MyNameIs


1 Answers

There is nothing wrong with your toolbar implementation.

A reason for the appbar_scrolling_view_behavior to not work is if there is an unsupported widget in the layout inflated by the fragment. Try to encapsulate your layout in a NestedScrollView, e.g.

 <android.support.v4.widget.NestedScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/ScrollView01"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <RelativeLayout 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:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:context=".MainActivity">
        /* Your views */
    </RelativeLayout>
</android.support.v4.widget.NestedScrollView>
like image 93
Sevle Avatar answered Feb 24 '26 08:02

Sevle



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!