Hide AppBar when scrolling down

后端 未结 7 1905
小蘑菇
小蘑菇 2020-12-02 18:10

I have an AppBar and a horizontal linearlayout (which includes an edit text and two imagebuttons) and other things in my layout. When user scrolls down,

相关标签:
7条回答
  • 2020-12-02 18:12

    You need to structure your layout like this:

    <android.support.design.widget.CoordinatorLayout >
        <android.support.design.widget.AppBarLayout >
            <android.support.design.widget.CollapsingToolbarLayout >
                <ImageView />
                <android.support.v7.widget.Toolbar />
            </android.support.design.widget.CollapsingToolbarLayout>
        </android.support.design.widget.AppBarLayout>
        <!-- Your scrollable content here -->
    </android.support.design.widget.CoordinatorLayout>
    

    Complete tutorial at: http://blog.grafixartist.com/toolbar-animation-with-android-design-support-library/

    0 讨论(0)
  • 2020-12-02 18:12

    In order to hide toolbar use the code below.

    toolbar.animate().translationY(-toolbar.getBottom()).setInterpolator(new AccelerateInterpolator()).start();
    
    0 讨论(0)
  • 2020-12-02 18:18

    I hope that this article will help you: https://guides.codepath.com/android/Handling-Scrolls-with-CoordinatorLayout

    Try using CoordinatorLayout and CollapsingToolbar.

    <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:expandedTitleMarginEnd="64dp"
            app:expandedTitleMarginStart="48dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">
    
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_scrollFlags="scroll|enterAlways">    
            </android.support.v7.widget.Toolbar>
    
    </android.support.design.widget.CollapsingToolbarLayout>
    
    0 讨论(0)
  • 2020-12-02 18:21

    check this out you need to set flag for it in CoordinatorLayout and CollapsingToolbar,

      <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.v4.widget.NestedScrollView
                 android:layout_width="match_parent"
                 android:layout_height="match_parent"
                 app:layout_behavior="@string/appbar_scrolling_view_behavior">
    
             <!-- Your scrolling content -->
    
         </android.support.v4.widget.NestedScrollView>
    
         <android.support.design.widget.AppBarLayout
                 android:layout_height="wrap_content"
                 android:layout_width="match_parent">
    
             <android.support.v7.widget.Toolbar
                     ...
                     app:layout_scrollFlags="scroll|enterAlways"/>
    
             <android.support.design.widget.TabLayout
                     ...
                     app:layout_scrollFlags="scroll|enterAlways"/>
    
         </android.support.design.widget.AppBarLayout>
    
     </android.support.design.widget.CoordinatorLayout>
    

    http://developer.android.com/reference/android/support/design/widget/AppBarLayout.html

    0 讨论(0)
  • 2020-12-02 18:22

    In the Drawer.Layout add in android:fitsSystemWindows="true".

    <android.support.v4.widget.DrawerLayout android:id="@+id/drawer_layout"
        .../...
        // add in
        android:fitsSystemWindows="true">
    

    It's better to separate out your concerns. Using the sample provided by chrisbanes/cheesesquare, your layout would be better looking like this:

    <android.support.v4.widget.DrawerLayout android:id="@+id/drawer_layout"
        .../...
        // add in
        android:fitsSystemWindows="true">
    
    <android.support.design.widget.CoordinatorLayout .../...>
    
    <android.support.design.widget.AppBarLayout .../...>
    
        <android.support.v7.widget.Toolbar 
            // Why are you using two themes?
            // android:theme="@style/ThemeOverlay.AppCompat.ActionBar"/>
    
    </android.support.design.widget.AppBarLayout>
    
    <android.support.v4.view.ViewPager 
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
    </android.support.design.widget.CoordinatorLayout>
    </android.support.v4.widget.DrawerLayout>
    

    Replace this:

        <LinearLayout .../...>
        <View .../...>
        </View>
        <LinearLayout .../...>
            <ImageButton
                .../...
                />
        </LinearLayout>
        <ScrollView .../...>
        <!--many views inside scrollview..... -->
        </ScrollView>
        </LinearLayout>
    

    With

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

    And then use the ViewPager to populate your list.

     ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
        if (viewPager != null) {
            setupViewPager(viewPager);
        }
    
    private void setupViewPager(ViewPager viewPager) {
        Adapter adapter = new Adapter(getSupportFragmentManager());
        adapter.addFragment(new CheeseListFragment(), "Category 1");
        adapter.addFragment(new CheeseListFragment(), "Category 2");
        adapter.addFragment(new CheeseListFragment(), "Category 3");
        viewPager.setAdapter(adapter);
    }
    

    Then you can format this CheeseListFragment to add your image views individually.

    It's also unnecessary to keep declaring your schema:

    xmlns:app="http://schemas.android.com/apk/res-auto"
    

    This is an indicator that you are copying and pasting code and not understanding it.

    You should also be using a nestedScrollView without the view, and linear layout above it. Mind you, it's not clear what you are trying to acheive with that.

    0 讨论(0)
  • 2020-12-02 18:28

    Try this code: Use layout_scrollFlags like below:

    app:layout_scrollFlags="scroll|enterAlways"

    and your XML file change like below:

    <?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"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="MainActivity">
    
    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">
    
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            app:layout_scrollFlags="scroll|enterAlways"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay"/>
    
        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabMode="fixed"
            app:tabGravity="fill"/>
    
    </android.support.design.widget.AppBarLayout>
    
    <include layout="@layout/content_main" />
    
    </android.support.design.widget.CoordinatorLayout>
    

    this is content_main XML file

    <android.support.v4.widget.NestedScrollView
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
    
    </LinearLayout>
    
    </android.support.v4.widget.NestedScrollView>
    
    0 讨论(0)
提交回复
热议问题