NestedScrollview +协调器布局+ SmartTabLayout具有带有分页的viewpager的滚动无限问题

时间:2018-08-04 07:49:15

标签: android android-coordinatorlayout android-appbarlayout android-nestedscrollview

下面是两张奇怪的图片

Layout Screenshot

NestedScrollview has infinite space and it scrolls and scrolls

<android.support.v4.widget.NestedScrollView 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="wrap_content"
android:fillViewport="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:layout_gravity="fill_vertical">


<android.support.design.widget.CoordinatorLayout 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:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:descendantFocusability="blocksDescendants">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        tools:ignore="ExtraText">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar_top"
            android:layout_width="match_parent"
            android:layout_height="?android:attr/actionBarSize"
            app:elevation="0dp"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:titleTextColor="@color/theme_color" />


        <android.support.v7.widget.CardView
            android:id="@+id/cardView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/cardviewLogin"
            android:layout_centerHorizontal="true"
            android:layout_gravity="center_horizontal"
            app:cardCornerRadius="@dimen/eighteen_dp"
            app:cardUseCompatPadding="true">


            <io.tnine.lifehacks_.customviews.MyTabLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:app="http://schemas.android.com/apk/res-auto"
                android:id="@+id/viewpagertab"
                android:layout_width="wrap_content"
                android:layout_height="@dimen/tab_height_"
                android:clipToPadding="false"
                android:paddingLeft="4dp"
                android:paddingRight="4dp"
                app:stl_defaultTabTextColor="@color/select_color"
                app:stl_defaultTabTextHorizontalPadding="18dp"
                app:stl_defaultTabTextSize="@dimen/sixteen"
                app:stl_dividerColor="@color/transparent_black_hex_11"
                app:stl_dividerThickness="0dp"
                app:stl_indicatorColor="@color/tab_background"
                app:stl_indicatorCornerRadius="18dp"
                app:stl_indicatorGravity="center"
                app:stl_indicatorInterpolation="smart"
                app:stl_indicatorThickness="36dp"
                app:stl_underlineColor="@color/transparent_black_hex_11"
                app:stl_underlineThickness="0dp" />

        </android.support.v7.widget.CardView>


    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
</android.support.v4.widget.NestedScrollView>

这是xml布局文件。我要实现的是,当用户在viewpager中包含的片段之一中滚动recyclerview时,整个屏幕应自动滚动,但同时nestedscrollview会滚动并滚动。请帮助。

2 个答案:

答案 0 :(得分:0)

可能是因为您在NestedScrollView的外部使用CoordinatorLayout

尝试在NestedScrollView内部使用CoordinatorLayout 内部 ViewPagerNestedScrollView


  

CoordinatorLayout是功能强大的FrameLayout

     

CoordinatorLayout用于两个主要用例:

     
      
  1. 作为顶级应用程序 装饰或镶边
  2.   
  3. 作为与一个或多个子视图进行特定交互的容器
  4.   

https://developer.android.com/reference/android/support/design/widget/CoordinatorLayout

看一下: ViewPager in a NestedScrollView

答案 1 :(得分:0)

您可以不使用NestedScrollView来完成这项工作。

使用以下代码:

<android.support.design.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">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/whiteColor">

        <android.support.design.widget.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fitsSystemWindows="true"
            app:contentScrim="@color/tab_background"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

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

                <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar_top"
                    android:layout_width="match_parent"
                    android:layout_height="?android:attr/actionBarSize"
                    app:elevation="0dp"
                    app:layout_scrollFlags="scroll|enterAlways"
                    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
                    app:titleTextColor="@color/theme_color" />

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="80dp"
                    android:layout_margin="@dimen/fourDp"
                    android:orientation="horizontal"
                    android:padding="@dimen/fourDp">

                    <ImageView
                        android:id="@+id/batman"
                        android:layout_width="50dp"
                        android:layout_height="50dp"
                        android:layout_gravity="center_vertical" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_vertical"
                        android:gravity="center_horizontal"
                        android:padding="@dimen/ten_dp"
                        android:text="@string/sign_in"
                        android:textSize="@dimen/sixteen" />

                    <ImageView
                        android:id="@+id/google"
                        android:layout_width="50dp"
                        android:layout_height="50dp"
                        android:layout_gravity="center_vertical" />

                </LinearLayout>

            </LinearLayout>
        </android.support.design.widget.CollapsingToolbarLayout>

        <android.support.v7.widget.CardView
            android:id="@+id/cardView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:cardCornerRadius="@dimen/eighteen_dp"
            app:cardUseCompatPadding="true">

            <io.tnine.lifehacks_.customviews.MyTabLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:app="http://schemas.android.com/apk/res-auto"
                android:id="@+id/viewpagertab"
                android:layout_width="match_parent"
                android:layout_height="@dimen/tab_height_"
                android:clipToPadding="false"
                android:paddingLeft="4dp"
                android:paddingRight="4dp"
                app:stl_defaultTabTextColor="@color/select_color"
                app:stl_defaultTabTextHorizontalPadding="18dp"
                app:stl_defaultTabTextSize="@dimen/sixteen"
                app:stl_distributeEvenly="true"
                app:stl_dividerColor="@color/transparent_black_hex_11"
                app:stl_dividerThickness="0dp"
                app:stl_indicatorColor="@color/tab_background"
                app:stl_indicatorCornerRadius="18dp"
                app:stl_indicatorGravity="center"
                app:stl_indicatorInterpolation="linear"
                app:stl_indicatorThickness="@dimen/fortydp"
                app:stl_underlineColor="@color/transparent_black_hex_11"
                app:stl_underlineThickness="0dp"
                app:tabGravity="fill"
                app:tabMode="scrollable" />
        </android.support.v7.widget.CardView>

    </android.support.design.widget.AppBarLayout>


    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />
    </android.support.design.widget.CoordinatorLayout>

但这不会使您的CollapsingToolbarLayout崩溃。 为此,您应该在RecyclerView中添加以下代码。

 hacksRecycler.isNestedScrollingEnabled = true

将nestedscrolling设置为true,瞧!