我正在使用一个 fragment 来显示评论和评分,我想添加一个效果,比如当我们向上或向下拉 fragment 时它会关闭或消失并带有动画,就像 facebook 评论的布局。
这是我的布局, 它包含一个 Recyclerview。
我希望当我向上或向下拉这个布局时,它像 facebook 评论布局那样关闭。
能否请您建议我一种实现此功能的方法。
最佳答案
一个简单的技巧(可能会根据您的用例起作用)是使用 BottomSheetBehaviour
来实现它。
优点:
- 不需要外部库(支持设计除外)
- 相对容易上手
- 大量的例子
- 易于调整动画,基于
public void onSlide(@NonNull View bottomSheet, float slideOffset)
您可以将 xml 结构化为如下 fragment :
<android.support.design.widget.CoordinatorLayout >
<android.support.design.widget.AppBarLayout >
<android.support.design.widget.CollapsingToolbarLayout >
<android.support.v7.widget.Toolbar />
</android.support.design.widget.CollapsingToolbarLayout >
</android.support.design.widget.AppBarLayout >
<include layout="@layout/content_layout" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="300dp"
android:fitsSystemWindows="true"
app:behavior_hideable="false"
app:behavior_peekHeight="0dp"
app:layout_behavior="@string/bottom_sheet_behavior">
<include layout="@layout/bottom_sheet_content_view" />
</FrameLayout>
</android.support.design.widget.CoordinatorLayout>
一些入门帮助:
https://medium.com/@nullthemall/new-bottomsheet-caab21aff19b
关于android - 如何在向上或向下拉动时关闭 fragment ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32873937/