不知何故,我遇到了Reddit应用程序,想知道他们的滚动机制是如何工作的。从gif中可以看到,有一个菜单栏(向上/向下投票共享栏)始终位于屏幕内,向上/向下滚动时无法滚动到屏幕外。向上滚动时,它将位于工具栏(顶部的灰色栏)下方。向下滚动时,它将位于 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
If I wanted to write this page, what dependencies, widgets or concepts would I need to use or look into?
注意:如果愿意,可以给我代码片段:)
最佳答案
您可以使用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>
关于android - 防止ScrollView中的子项被滚动出屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59871778/