简体   繁体   中英

Handling scroll of AppBarLayout not working(ToolBar doesn't collapse)

I am trying to collapse the toolBar + Image and stick it to a minimum height. Its's not working, so posting my code. Any help will be appreciated.

using these below links for reference

https://guides.codepath.com/android/Handling-Scrolls-with-CoordinatorLayout#expanding-and-collapsing-toolbars

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

below is my code for 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:fitsSystemWindows="true">

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="192dp"
    android:fitsSystemWindows="true"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <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/anim_toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
        <!--            app:layout_collapseMode="pin"
        -->

        <ImageView
            android:id="@+id/header"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/header"
            android:fitsSystemWindows="true"
            android:minHeight="100dp"
            android:scaleType="centerCrop"
            app:layout_collapseMode="parallax"
            app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed"/>
    </android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>

<android.support.v7.widget.RecyclerView
    android:id="@+id/scrollableview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

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

and the class code is

public class GroupChatDetailsActivity extends AppCompatActivity {


    int mutedColor = R.attr.colorPrimary;
    GroupChatDetailsAdapter groupChatDetailsAdapter;
    private CollapsingToolbarLayout collapsingToolbar;
    private RecyclerView recyclerView;

    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        setContentView (R.layout.group_details_layout);
        setup ();
    }

    private void setup () {
        Toolbar toolbar = (Toolbar) findViewById (R.id.anim_toolbar);
        setSupportActionBar (toolbar);
        getSupportActionBar ().setDisplayHomeAsUpEnabled (true);

        collapsingToolbar = (CollapsingToolbarLayout) findViewById (R.id.collapsing_toolbar);
        collapsingToolbar.setTitle ("Test Title");

        ImageView header = (ImageView) findViewById (R.id.header);
        Bitmap bitmap = BitmapFactory.decodeResource (getResources (),
                R.drawable.header);

        Palette.from (bitmap).generate (new Palette.PaletteAsyncListener () {
            @SuppressWarnings ("ResourceType")
            @Override
            public void onGenerated (Palette palette) {
                mutedColor = palette.getMutedColor (R.color.primary_500);
                collapsingToolbar.setContentScrimColor (mutedColor);
                collapsingToolbar.setStatusBarScrimColor (R.color.black_trans80);
            }
        });

        recyclerView = (RecyclerView) findViewById (R.id.scrollableview);
        recyclerView.setHasFixedSize (true);
        LinearLayoutManager layoutManager
                = new LinearLayoutManager (this);
        recyclerView.setLayoutManager (layoutManager);

        List<String> listData = new ArrayList<String> ();
        int ct = 0;
        for (int i = 0; i < VersionModel.data.length * 2; i++) {
            listData.add (VersionModel.data[ct]);
            ct++;
            if (ct == VersionModel.data.length) {
                ct = 0;
            }
        }
        if (groupChatDetailsAdapter == null) {
            groupChatDetailsAdapter = new GroupChatDetailsAdapter (listData);
            recyclerView.setAdapter (groupChatDetailsAdapter);
        }
    }
}

Not sure why but the fitSystemWindows on your CoordinatorLayout is breaking this. Put your CoordinatorLayout inside a FrameLayout (and move the fitSystemWindows=true to the FrameLayout).

you do not need to provide app:layout_scrollFlags to your ToolBar & ImageView as its already set via CollapsingToolbarLayout

Also, you can set app:layout_collapseMode to your ToolBar as "pin", "parallax", or "none" as per your requirement.

Also, you have not provide content of CoordinatorLayout just make sure your namespace defined correctly as below.

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

I hope it will help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM