gpt4 book ai didi

android - 如何根据滚动方向折叠/展开 View ?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:49:24 24 4
gpt4 key购买 nike

我在 LinearLayout 中有一个 View 和一个 RecyclerView。我想要实现的是这样的:

https://material.google.com/patterns/scrolling-techniques.html#scrolling-techniques-behavior

基本上,当我向上滚动 RecyclerView 时,View 会折叠。如果我向下滚动 RecyclerView,它会展开。

我已经尝试过各种方法,但如果手指在滚动位置周围猛拉,动画就会断断续续。只有当手指在一个方向上有意识地滚动时,它才能很好地动画。我该如何正确执行此操作?

最佳答案

您必须将 Coordinator Layout 与 CollapsingToolbarLayout 一起使用

<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:clipToPadding="false">

<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="210dp"
android:stateListAnimator="@animator/appbar_always_elevated" //I put this here because I want to have shadow when is open, but you have to create the xml file.
android:background="@color/WHITE">

<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:collapsedTitleTextAppearance="@style/TextAppearance.AppCompat.Widget.ActionBar.Title.Inverse"
app:expandedTitleMarginStart="72dp"
app:expandedTitleTextAppearance="@style/TextAppearance.AppCompat.Widget.ActionBar.Title.Inverse"
app:layout_scrollFlags="scroll|exitUntilCollapsed"> //HERE you should take a look what you want your collapse bar do.

<Here you put the content for you collapse bar, like a ImageView>

<android.support.v7.widget.Toolbar //This is the size of your fixed bar when you collapse, even here you can put a back button, for example
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="40dp"
app:layout_collapseMode="pin" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>

<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/main_home_list_swipe"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" >

<android.support.v7.widget.RecyclerView
android:id="@+id/main_home_list"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

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

Obs:如果你放在一个xml文件中,注释会出错。正在提议,所以你会记得阅读哈哈哈

关于android - 如何根据滚动方向折叠/展开 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39983970/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com