简体   繁体   English

AppBarLayout的滚动处理不起作用(ToolBar不会折叠)

[英]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. 我正在尝试折叠toolBar + Image并将其粘贴到最小高度。 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 https://guides.codepath.com/android/Handling-Scrolls-with-CoordinatorLayout#expanding-and-collapsing-toolbars

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

below is my code for xml 以下是我的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. 不知道为什么,但是CoordinatorLayout上的fitSystemWindows打破了这一点。 Put your CoordinatorLayout inside a FrameLayout (and move the fitSystemWindows=true to the FrameLayout). 将您的CoordinatorLayout放在FrameLayout内(并将fitSystemWindows = true移至FrameLayout)。

you do not need to provide app:layout_scrollFlags to your ToolBar & ImageView as its already set via CollapsingToolbarLayout 您不需要向ToolBarImageView提供app:layout_scrollFlags ,因为已经通过CollapsingToolbarLayout对其进行了设置

Also, you can set app:layout_collapseMode to your ToolBar as "pin", "parallax", or "none" as per your requirement. 另外,您可以根据需要将app:layout_collapseMode设置为ToolBar的“ pin”,“ parallax”或“ none”。

Also, you have not provide content of CoordinatorLayout just make sure your namespace defined correctly as below. 另外,您没有提供CoordinatorLayout内容,只是确保正确定义了您的命名空间,如下所示。

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

I hope it will help. 希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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