简体   繁体   English

防止 ScrollView 中的孩子被滚出屏幕

[英]Prevent a child in ScrollView from being scrolled out of the screen

Somehow I came across Reddit app, and wondered how their Scrolling mechanism works.不知何故,我遇到了 Reddit 应用程序,并想知道他们的滚动机制是如何工作的。 As you can see from the gif, there is a Menubar (Up/Downvote-comment-share bar) that always locates within the screen and can't be scrolled out of the screen when scrolled up/down.从 gif 图中可以看出,有一个菜单栏(向上/向下投票-评论-共享栏)始终位于屏幕内,向上/向下滚动时无法滚动出屏幕。 When scrolling up, it will be located underneath the Toolbar (the grey bar at the top) .向上滚动时,它将位于工具栏下方(顶部的灰色栏) When scrolling down, it will be located above the EditTextView (the Add-a-comment bar at the bottom) .向下滚动时,它将位于EditTextView上方(底部的添加注释栏)

Reddit 滚动机制

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

If I wanted to write this page, what dependencies, widgets or concepts would I need to use or look into?如果我想编写此页面,我需要使用或研究哪些依赖项、小部件或概念?

Note: You can give me snippets of codes if you prefer :)注意:如果您愿意,可以给我代码片段:)

you can use the CoordinatorLayout layout to realize that function.您可以使用CoordinatorLayout布局来实现该功能。 the layout like that.这样的布局。 for details, you can see this blog in the medium.有关详细信息,您可以在媒体中查看此博客


<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>

The toolbar (containing the menu) is outside of the ScrollView in the XML.工具栏(包含菜单)在 XML 中的 ScrollView 之外。

Or it may not even be a ScrollView at all.或者它甚至可能根本不是 ScrollView。

It may just be view recycling- but even so the toolbar is outside of that XML layout used for displaying the content.它可能只是视图回收——但即便如此,工具栏也在用于显示内容的 XML 布局之外。

For hiding the TopBar and bottomBar (comment box) I recommend you use Animation为了隐藏 TopBar 和 bottomBar(评论框),我建议您使用Animation

Simply call animation like简单地将动画称为

//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

Implement this will both Views using addOnScrollListener() or I suggest you to have a look at this answer for implementing scroll Up/Down using CoordinateLayout使用addOnScrollListener()实现这两个视图,或者我建议您查看此答案以使用CoordinateLayout实现scroll Up/Down

Hope this will help!希望这会有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM