处理AppBarLayout的滚动不起作用(ToolBar不会崩溃)

时间:2015-09-30 12:23:23

标签: android android-toolbar android-appbarlayout

我正在尝试折叠toolBar + Image并将其粘贴到最小高度。 它不起作用,所以发布我的代码。任何帮助将不胜感激。

使用以下链接作为参考

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

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

下面是我的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>

,类代码是

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);
        }
    }
}

2 个答案:

答案 0 :(得分:0)

不确定为什么但CoordinatorLayout上的fitSystemWindows正在打破这个问题。将CoordinatorLayout放在FrameLayout中(并将fitSystemWindows = true移动到FrameLayout)。

答案 1 :(得分:-1)

您无需向ToolBar&amp;提供ImageViewCollapsingToolbarLayout已经通过app:layout_collapseMode

设置了CoordinatorLayout

此外,您可以将xmlns:app="http://schemas.android.com/apk/res-auto" 设置为工具栏为&#34; pin&#34;,&#34; parallax&#34;或&#34; none&#34;根据您的要求。

此外,您尚未提供{{1}}的内容,只需确保您的命名空间正确定义如下。

{{1}}

我希望它会有所帮助。