Android : 如何在滚动时将中间布局贴在顶部
Android : How to stick middle layout on top at the time of scrolling
当中间布局(黄色视图)出现在顶部时,当我垂直滚动时,它应该停留在顶部位置并滚动底部布局的其余部分。
我不知道如何激活请帮助我
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<View
android:id="@+id/viewFirst"
android:layout_width="match_parent"
android:layout_height="400dp"
android:background="#990000" />
<View
android:id="@+id/viewMiddle"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#ddaa00" />
<View
android:id="@+id/viewLast"
android:layout_width="match_parent"
android:layout_height="500dp"
android:background="#0000AA" />
</LinearLayout>
</ScrollView>
这可能不是您想要的,但它可以帮助您做您想做的事。
您可以使用包含 AppBarLayout
的 CoordinatorLayout
和其他一些滚动视图,例如在您的情况下 ScrollView
AppBarLayout
包含您的 viewFirst
和 viewFirst2
viewFirst
必须包装在 CollapsingToolbarLayout
中,并设置了 app:layout_scrollFlags="scroll|exitUntilCollapsed"
属性
滚动视图必须设置 app:layout_behavior="@string/appbar_scrolling_view_behavior"
属性才能实现 CoordintatorLayout
功能。
如果您要使用 ScrollView
,则必须使用 android:nestedScrollingEnabled="true"
来启用 CoordintatorLayout
的滚动行为以及 ScrollView
本身。
android:nestedScrollingEnabled="true"
仅适用于 API-21
或者,您可以使用 NestedScrollView
或任何其他滚动视图,例如 RecyclerView
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<View
android:id="@+id/viewFirst"
android:layout_width="match_parent"
android:layout_height="400dp"
android:background="#990000" />
</com.google.android.material.appbar.CollapsingToolbarLayout>
<View
android:id="@+id/viewMiddle"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#ddaa00" />
</com.google.android.material.appbar.AppBarLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:nestedScrollingEnabled="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<TextView
android:id="@+id/viewLast"
android:layout_width="match_parent"
android:layout_height="500dp"
android:background="#0000AA"
android:text="
Lorem ipsum dolor sit amet, ipsum auctor magna scelerisque. Dignissim ante a, eros vel et donec, eu bibendum in donec diam nec, venenatis quam vel est et lacinia sem, nunc ac qui in eu vel pede. Nec duis, nullam arcu iaculis ligula congue urna in, malesuada lorem mauris, nonummy malesuada diam et congue, laoreet in quis. Arcu suspendisse parturient erat, aliquet aliquam maecenas proin vel faucibus cras, per vestibulum ut id tellus. Ridiculus quis luctus praesent fusce, iaculis amet nunc nisl. Tortor proin curabitur vestibulum, ut mi maecenas nisl diam, ligula amet erat per vel risus, molestie a tempor ut integer, volutpat porttitor lectus adipisicing elit egestas. Tellus ullamcorper fusce nullam donec urna, proin purus est vitae dui suspendisse erat, nullam posuere gravida nibh odio elementum non.
Mi ac proin suspendisse eu, in dui. A maecenas, quam cras dictum lorem libero nibh venenatis. Tristique ac, ligula eget volutpat, nascetur nec. Luctus commodi, mi volutpat ante consectetuer purus dictumst wisi, laoreet ut sem molestie blandit sed. Mollis odio neque, erat et tortor vel nonummy. Morbi luctus eget laoreet tortor vitae ligula, aliquam a nisl amet magna. Pellentesque lorem feugiat pellentesque euismod eget, id vel arcu ipsum morbi, feugiat sed non erat integer, non at proin aenean nunc ultrices, orci eu. Vulputate aliquam rutrum, elit nibh, ac cras ut nisl sed et interdum, gravida suspendisse sed aenean, habitant in lectus sit. Dolor vivamus in a ut, nec adipiscing. Quis consectetuer, accumsan tempor elit fusce sit et.
Feugiat suspendisse cras erat ligula cursus augue, lectus est elementum mauris etiam fermentum ornare. Rhoncus donec blandit fermentum iure elit fusce. Nec ornare. Vestibulum praesent, eget lacinia eget phasellus nullam hendrerit id. Dignissim aliquet, eu per deserunt quam enim adipiscing, urna lacus eros adipiscing, vestibulum eget imperdiet, sagittis lectus lorem lobortis morbi. Eget per enim nulla metus amet nulla. Sit commodo varius sagittis non, sed ipsum, orci eu ut egestas eum suscipit, nulla nullam congue praesent, quis faucibus. Lacus vestibulum ipsum eget lobortis blandit, pellentesque wisi tortor vestibulum tempor laoreet. Augue vivamus in consequat nunc a sem, justo ultricies cras lacinia, dui posuere ultrices facilisis, nibh nunc vel in tempus, ac diam donec sit urna. Sed eleifend hendrerit magna, sed vestibulum nec vivamus massa. Phasellus sodales in ut consequat dictum sapien, eu erat cras tellus. Sed provident, eget aenean, etiam egestas ac sed ac.
Auctor a etiam, ipsum venenatis sociosqu ut elementum sodales et, venenatis a libero porro vehicula. Arcu dictum sed tempor, libero aliquam, aenean viverra congue eros sed. Etiam aenean eleifend sit mauris, wisi pellentesque massa non urna suscipit. Nisl sit euismod cursus lectus. Lacus ligula ipsum quam dui curabitur eros. At aliquam torquent varius proin sociosqu, mi id fringilla nec."
android:textColor="#fff" />
</ScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
预览
您还可以查看 this library 作为替代解决方案。
当中间布局(黄色视图)出现在顶部时,当我垂直滚动时,它应该停留在顶部位置并滚动底部布局的其余部分。
我不知道如何激活请帮助我
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<View
android:id="@+id/viewFirst"
android:layout_width="match_parent"
android:layout_height="400dp"
android:background="#990000" />
<View
android:id="@+id/viewMiddle"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#ddaa00" />
<View
android:id="@+id/viewLast"
android:layout_width="match_parent"
android:layout_height="500dp"
android:background="#0000AA" />
</LinearLayout>
</ScrollView>
这可能不是您想要的,但它可以帮助您做您想做的事。
您可以使用包含 AppBarLayout
的 CoordinatorLayout
和其他一些滚动视图,例如在您的情况下 ScrollView
AppBarLayout
包含您的viewFirst
和viewFirst2
viewFirst
必须包装在CollapsingToolbarLayout
中,并设置了app:layout_scrollFlags="scroll|exitUntilCollapsed"
属性滚动视图必须设置
app:layout_behavior="@string/appbar_scrolling_view_behavior"
属性才能实现CoordintatorLayout
功能。
如果您要使用 ScrollView
,则必须使用 android:nestedScrollingEnabled="true"
来启用 CoordintatorLayout
的滚动行为以及 ScrollView
本身。
android:nestedScrollingEnabled="true"
仅适用于 API-21
或者,您可以使用 NestedScrollView
或任何其他滚动视图,例如 RecyclerView
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<View
android:id="@+id/viewFirst"
android:layout_width="match_parent"
android:layout_height="400dp"
android:background="#990000" />
</com.google.android.material.appbar.CollapsingToolbarLayout>
<View
android:id="@+id/viewMiddle"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#ddaa00" />
</com.google.android.material.appbar.AppBarLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:nestedScrollingEnabled="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<TextView
android:id="@+id/viewLast"
android:layout_width="match_parent"
android:layout_height="500dp"
android:background="#0000AA"
android:text="
Lorem ipsum dolor sit amet, ipsum auctor magna scelerisque. Dignissim ante a, eros vel et donec, eu bibendum in donec diam nec, venenatis quam vel est et lacinia sem, nunc ac qui in eu vel pede. Nec duis, nullam arcu iaculis ligula congue urna in, malesuada lorem mauris, nonummy malesuada diam et congue, laoreet in quis. Arcu suspendisse parturient erat, aliquet aliquam maecenas proin vel faucibus cras, per vestibulum ut id tellus. Ridiculus quis luctus praesent fusce, iaculis amet nunc nisl. Tortor proin curabitur vestibulum, ut mi maecenas nisl diam, ligula amet erat per vel risus, molestie a tempor ut integer, volutpat porttitor lectus adipisicing elit egestas. Tellus ullamcorper fusce nullam donec urna, proin purus est vitae dui suspendisse erat, nullam posuere gravida nibh odio elementum non.
Mi ac proin suspendisse eu, in dui. A maecenas, quam cras dictum lorem libero nibh venenatis. Tristique ac, ligula eget volutpat, nascetur nec. Luctus commodi, mi volutpat ante consectetuer purus dictumst wisi, laoreet ut sem molestie blandit sed. Mollis odio neque, erat et tortor vel nonummy. Morbi luctus eget laoreet tortor vitae ligula, aliquam a nisl amet magna. Pellentesque lorem feugiat pellentesque euismod eget, id vel arcu ipsum morbi, feugiat sed non erat integer, non at proin aenean nunc ultrices, orci eu. Vulputate aliquam rutrum, elit nibh, ac cras ut nisl sed et interdum, gravida suspendisse sed aenean, habitant in lectus sit. Dolor vivamus in a ut, nec adipiscing. Quis consectetuer, accumsan tempor elit fusce sit et.
Feugiat suspendisse cras erat ligula cursus augue, lectus est elementum mauris etiam fermentum ornare. Rhoncus donec blandit fermentum iure elit fusce. Nec ornare. Vestibulum praesent, eget lacinia eget phasellus nullam hendrerit id. Dignissim aliquet, eu per deserunt quam enim adipiscing, urna lacus eros adipiscing, vestibulum eget imperdiet, sagittis lectus lorem lobortis morbi. Eget per enim nulla metus amet nulla. Sit commodo varius sagittis non, sed ipsum, orci eu ut egestas eum suscipit, nulla nullam congue praesent, quis faucibus. Lacus vestibulum ipsum eget lobortis blandit, pellentesque wisi tortor vestibulum tempor laoreet. Augue vivamus in consequat nunc a sem, justo ultricies cras lacinia, dui posuere ultrices facilisis, nibh nunc vel in tempus, ac diam donec sit urna. Sed eleifend hendrerit magna, sed vestibulum nec vivamus massa. Phasellus sodales in ut consequat dictum sapien, eu erat cras tellus. Sed provident, eget aenean, etiam egestas ac sed ac.
Auctor a etiam, ipsum venenatis sociosqu ut elementum sodales et, venenatis a libero porro vehicula. Arcu dictum sed tempor, libero aliquam, aenean viverra congue eros sed. Etiam aenean eleifend sit mauris, wisi pellentesque massa non urna suscipit. Nisl sit euismod cursus lectus. Lacus ligula ipsum quam dui curabitur eros. At aliquam torquent varius proin sociosqu, mi id fringilla nec."
android:textColor="#fff" />
</ScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
预览
您还可以查看 this library 作为替代解决方案。