android - 如何在向上或向下拉动时关闭 fragment ?

标签 android android-layout animation android-fragments

我正在使用一个 fragment 来显示评论和评分,我想添加一个效果,比如当我们向上或向下拉 fragment 时它会关闭或消失并带有动画,就像 facebook 评论的布局。

这是我的布局, 它包含一个 Recyclerview。

enter image description here

我希望当我向上或向下拉这个布局时,它像 facebook 评论布局那样关闭。

enter image description here

能否请您建议我一种实现此功能的方法。

最佳答案

一个简单的技巧(可能会根据您的用例起作用)是使用 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

https://code.tutsplus.com/articles/how-to-use-bottom-sheets-with-the-design-support-library--cms-26031

关于android - 如何在向上或向下拉动时关闭 fragment ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32873937/

相关文章:

android - 以编程方式在 View 上设置负边距

javascript - 当两个预构建的 jQuery 动画序列都完成时,如何通过一个回调并行运行它们?

javascript - 播放动画然后加载下一页

android - 将 2 个 fragment (一个 fragment 和一个 ListFragment)作为一个滚动

c# - 适用于 Android 的绑定(bind)库 Mono

Hudson/Jenkins 上的 Android Logcat 文件

java - Android studio 不同屏幕尺寸像素

Android将按钮排列成一行

animation - D3路径动画的变化速度

java - 错误 : 'cannot find symbol class GetTokenResult' after adding latest firebase-messaging dependency