如何在滚动tablayout安卓版时滚动整个活动?

问题描述 投票:0回答:1

我如何在滚动tablayout时滚动整个活动?

例如:当我滚动我的tabLayout时,整个活动应该滚动...而不仅仅是tablayout的viewpager部分。

活动应该只在tablayout被滚动时滚动。

这就是我想要的......(请注意当tablayout滚动时,整个活动如何滚动......)。

image1

这就是我的情况......(注意,当tablayout滚动时,只有tablayout碎片部分会滚动)

image2

这是我试过的...因为我读过....

1. 在包含tablayout的布局活动中添加ScrollView。

2 将NestedScrollView添加到片段布局中。

图片_活动.xml - 活动中包含了tablayout和viewpager。

<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 - 在tablayout中选中的当前标签的片段。

<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>
android xml android-layout android-tablayout
1个回答
1
投票

你可以使用 CollapsingToolbarLayout

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="com.google.android.material.appbar.AppBarLayout$Behavior"
    xmlns:app="http://schemas.android.com/apk/res-auto">

   <com.google.android.material.appbar.AppBarLayout
       android:layout_width="match_parent"
       android:layout_height="300dp"
      >

       <com.google.android.material.appbar.CollapsingToolbarLayout
           android:id="@+id/collapsingToolbar"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:fitsSystemWindows="true"
           app:contentScrim="?attr/colorPrimary"
           app:expandedTitleMarginEnd="56dp"
           app:expandedTitleMarginStart="40dp"
           app:layout_scrollFlags="scroll|exitUntilCollapsed"
           app:title="Hello MindOrks!"
           >

           <!-- Put your top content here -->

           <LinearLayout
               android:id="@+id/toolbarImage"
               android:layout_width="match_parent"
               android:layout_height="match_parent"
               android:fitsSystemWindows="true"
               android:scaleType="centerCrop"
               android:src="@drawable/ic_android_black_24dp"
               app:layout_collapseMode="pin" >

               <TextView
                   android:layout_width="match_parent"
                   android:gravity="center"
                   android:text="Sample Top Content"
                   android:layout_gravity="center"
                   android:textSize="25dp"
                   android:textColor="@color/white"
                   android:layout_height="wrap_content"/>

           </LinearLayout>


       </com.google.android.material.appbar.CollapsingToolbarLayout>

   </com.google.android.material.appbar.AppBarLayout>

    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior">

        <LinearLayout
            android:layout_width="match_parent"
            android:orientation="vertical"
            android:layout_height="wrap_content">


            <com.google.android.material.tabs.TabLayout
                android:layout_width="match_parent"
                android:fitsSystemWindows="true"
                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="Top"/>

                <com.google.android.material.tabs.TabItem
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/recent"
                    android:text="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" />

        </LinearLayout>

    </androidx.core.widget.NestedScrollView>


</androidx.coordinatorlayout.widget.CoordinatorLayout>

它将看起来像这样。enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.