【问题标题】:Prevent a child in ScrollView from being scrolled out of the screen防止 ScrollView 中的孩子被滚动出屏幕
【发布时间】:2020-01-23 14:06:31
【问题描述】:

不知何故,我偶然发现了 Reddit 应用程序,并想知道他们的滚动机制是如何工作的。从 gif 中可以看到,有一个 Menubar(Up/Downvote-comment-share bar),它始终位于屏幕内且无法滚动向上/向下滚动时的屏幕。向上滚动时,它将位于工具栏(顶部的灰色栏)下方。向下滚动时,它将位于EditTextView(底部的添加评论栏)上方。

Relative layout

|-->Toolbar    (android:id="@+id/toolbar")

|-->ScrollView (android:layout_below="@id/toolbar")

    |-->Child (This child is located underneath the Toolbar when scrolling up)

    |-->Child

    |-->Child

如果我想写这个页面,需要什么依赖、小部件或概念 我需要使用或研究吗?

注意:如果你愿意,可以给我 sn-ps 的代码:)

【问题讨论】:

    标签: android kotlin android-recyclerview nestedrecyclerview stickyrecycleview


    【解决方案1】:

    您可以使用CoordinatorLayout 布局来实现该功能。这样的布局。详情可以在媒体中看到这个blog。

    
    <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/behavior_demo_coordinatorLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">
    
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_scrollFlags="scroll|enterAlways|snap"
            android:background="?attr/colorPrimary" />
    </android.support.design.widget.AppBarLayout>
    
    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/behavior_demo_swipe_refresh"
        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/behavior_demo_recycler"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            />
    </android.support.v4.widget.SwipeRefreshLayout>
    </android.support.design.widget.CoordinatorLayout>
    

    【讨论】:

    • 如果我想锁定屏幕内的菜单栏(Up/Downvote-comment-share bar),以及向上滚动时如何将位于工具栏(顶部的灰色栏)下方,好吗?
    • 可以,在工具栏中设置。你可以阅读博客,它有一些相同的例子
    • 我以为它只是为了最小化工具栏。我现在正在调查。谢谢你:)
    【解决方案2】:

    工具栏(包含菜单)在 XML 中的 ScrollView 之外。

    或者它甚至可能根本不是 ScrollView。

    这可能只是视图回收——但即便如此,工具栏仍位于用于显示内容的 XML 布局之外。

    【讨论】:

      【解决方案3】:

      为了隐藏 TopBar 和 bottomBar(评论框)我推荐你使用Animation

      简单的调用动画就好了

      //on scrollUp
      topLayout.animate().translationY(-300).alpha(0.0f).setDuration(300);
      // this will make the layout go up and fade to invisible
      
      //on scrollDown
      topLayout.animate().translationY(0).alpha(1.0f).setDuration(300);
      // this will make the layout come down and fade to be visible
      

      使用addOnScrollListener() 来实现这两个视图,或者我建议你看看这个answer 以使用CoordinateLayout 实现scroll Up/Down

      希望这会有所帮助!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-07-24
        • 1970-01-01
        相关资源
        最近更新 更多