【发布时间】:2020-10-07 04:26:56
【问题描述】:
如何在滚动标签布局时滚动整个活动?
例如 - 当我滚动我的 tabLayout 时 - 整个活动应该滚动......而不仅仅是 tablayout 的 viewpager 部分......
Activity 应该只在 tablayout 被缩放时滚动。
这就是我想要的……(注意整个活动如何滚动……当 tablayout 滚动时)
这就是我所拥有的......(注意当 tablayout 滚动时只有 tablayout 片段部分如何滚动)
这是我尝试过的……正如我所读到的……
1. 将 ScrollView 添加到包含 tablayout 的布局活动中
2 将 NestedScrollView 添加到片段布局中。
photo_activity.xml - 包含 tablayout 和 viewpager 的 Activity
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.tabs.TabLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/yearTabLayout"
app:tabIndicatorColor="@android:color/black">
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/top"
android:text="@string/top"/>
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/recent"
android:text="@string/recent"/>
</com.google.android.material.tabs.TabLayout>
<androidx.viewpager2.widget.ViewPager2
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/yearTabLayout"
android:id="@+id/year_view_pager" />
</RelativeLayout>
</ScrollView>
fragment_photos.xml - 选项卡布局中当前选定选项卡的片段。
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data class="TopPhotosActivityBinding" />
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:id="@+id/progressBar"/>
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/swipeRefreshTopPhotos">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/topPhotosRecyclerView"
tools:listitem="@layout/top_photos_year">
</androidx.recyclerview.widget.RecyclerView>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</RelativeLayout>
</androidx.core.widget.NestedScrollView>
</layout>
【问题讨论】:
-
我不知道细节,因为我从未使用过它,但我认为这是可以通过协调器布局实现的,看看它是如何工作的 -> guides.codepath.com/android/… 和 blog.mindorks.com/using-coordinator-layout-in-android
标签: android xml android-layout android-tablayout