1

我正在尝试折叠 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);
        }
    }
}
4

2 回答 2

0

不知道为什么,但是 CoordinatorLayout 上的 fitSystemWindows 打破了这一点。将您的 CoordinatorLayout 放在 FrameLayout 中(并将 fitSystemWindows=true 移动到 FrameLayout)。

于 2016-11-21T11:56:26.723 回答
-1

你不需要提供app:layout_scrollFlags给你的ToolBar&ImageView因为它已经通过CollapsingToolbarLayout

此外,您可以根据app:layout_collapseMode需要将 ToolBar 设置为“pin”、“parallax”或“none”。

此外,您还没有提供内容,CoordinatorLayout只是确保您的命名空间正确定义如下。

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

我希望它会有所帮助。

于 2015-09-30T12:43:10.507 回答