【问题标题】:ViewPager in a NestedScrollViewNestedScrollView 中的 ViewPager
【发布时间】:2015-06-01 18:48:03
【问题描述】:

我需要创建一个类似于 Google Newsstand 的界面,它是一种在折叠标题(垂直滚动)上的 ViewPager(水平滚动)。我的要求之一是使用 Google IO 2015 上展示的新设计支持库。 (http://android-developers.blogspot.ca/2015/05/android-design-support-library.html)

根据 Chris Banes (https://github.com/chrisbanes/cheesesquare) 创建的示例,我已经达到了能够进行折叠行为但使用基本 LinearLayout(没有水平滚动)的程度。

我试图用 ViewPager 替换 LinearLayout,但我得到了一个空白屏幕。我玩过:宽度、重量和各种视图组,但.... 仍然是一个空白屏幕。看来 ViewPager 和 NestedScrollView 互不喜欢。

我尝试了使用 Horizo​​ntalScrollView 的解决方法:它可以工作,但我失去了 PagerTitleStrip 功能的好处和对单个面板的关注(我可以在 2 个面板之间停止水平)。

现在我没有更多的想法,如果有人可以引导我找到解决方案......

谢谢

这是我最新的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<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="@dimen/header_height"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleMarginEnd="64dp"
            app:expandedTitleMarginStart="48dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <include
                layout="@layout/part_header"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_collapseMode="parallax"/>

            <android.support.v7.widget.Toolbar
                android:id="@+id/activity_main_toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

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

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/activity_main_nestedscrollview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <android.support.v4.view.ViewPager
            android:id="@+id/activity_main_viewpager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#FFA0"/>


    </android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>

【问题讨论】:

  • 您是否尝试将您的 ViewPager 包裹在 LinearLayout 中?只是好奇这是否会正确呈现ViewPager
  • 是的,我试过了,但结果还是一样。我对许多组件进行了许多测试,但无论层次结构如何,ViewPager 都不会在 NestedScrollView 中呈现
  • 我刚刚创建了一个测试应用程序,其中 ViewPagerNestedScrollView 内作为唯一的布局 - 似乎工作正常。我也能够成功进入DrawerLayout。也许您的代码的另一个方面阻止了您想要的功能。愿意分享更多您的资源吗?
  • 哇,你可以向左/向右和向上/向下滑动?我只是使用 FragmentPagerAdapter 在寻呼机中显示片段。你可以分享你的示例代码吗?
  • 我在这里粘贴了一个简单的示例布局:pastebin.com/jXPw6mtf 编辑:您可以在ViewPager 中加载您想要的任何片段。我可以上下滚动视图,也可以左右滚动ViewPager

标签: android android-layout android-viewpager android-support-library


【解决方案1】:

我遇到了这个问题,我通过setFillViewport (true)解决了

<android.support.v4.widget.NestedScrollView
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:id="@+id/nest_scrollview"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="mio.kon.yyb.nestedscrollviewbug.ScrollingActivity"
tools:showIn="@layout/activity_scrolling">


    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

活动中

  NestedScrollView scrollView = (NestedScrollView) findViewById (R.id.nest_scrollview);
    scrollView.setFillViewport (true);

【讨论】:

  • 请注意,这也可以在 XML 中设置,只需将 android:fillViewport="true" 添加到 NestedScrollView 对象。 documentation 将此属性描述为 Defines whether the scrollview should stretch its content to fill the viewport.
  • 这并不能解决占位符片段中的滚动问题
  • 谢谢!我坐在android:fillViewport="true"NestedScrollViewfragment 里面我有一个listView 我坐在android:nestedScrollingEnabled="true" .. 修复了滚动问题
  • @Krøllebølle NestedScrollView 没有这个属性,你必须以编程方式设置它。
  • 是的,这对我有用。当我在 NestedScrollView 中将 viewpager 的高度设置为 match_parent 时出现问题,它没有显示视图。
【解决方案2】:

好的,我用ViewPagerNestedScrollView 做了一个小演示。我面临的问题是ViewPagerListView 的高度。所以我对ListViewViewPager's 高度测量做了一些修改。

如果有人想查看代码,这里是链接:https://github.com/TheLittleNaruto/SupportDesignExample/

输出:

【讨论】:

  • 如果片段没有列表视图,则不起作用,如果所有片段的项目数相同,即使列表视图也起作用。
  • @Dev 问题在于 View Pager 的高度,因此,无论它在第一个片段中采用的高度如何,其他片段的高度也将相同。在这种情况下,您必须找出一种方法来动态设置 ViewPager 的高度,同时考虑每个片段的项目集。
  • 我可能会有点迟到了,但是,我今天正在处理这个问题,并且我已经(主要)添加了 app:layout_behavior=" @string/appbar_scrolling_view_behavior" 在 ViewPager 上每个 Fragment 的父视图。
  • 毫无疑问这是可行的,但这不是使问题过于复杂吗?我通过摆脱NestedScrollView 并改用LinearLayout 解决了这个问题。如果整个目的是滚动 ViewPager 中的内容,那么使用 NestedScrollView 而不是 LinearLayout 有什么好处?如果我错了,请纠正我。 :x 谢谢
  • 非常感谢!我正要拔掉头发,而您的 WrapContentHeightViewPager 就像一个魅力。我欠你一个!
【解决方案3】:

在您的 NestedScrollView 中添加 android:fillViewport="true" 。期间。

【讨论】:

  • 它解决了这个问题,但viewpager高度将是匹配父级(它占用整个页面高度)。
【解决方案4】:

不要将 ViewPager 放在 NestedScrollView 中,而是反过来。

将 NestedScrollView 放置在 ViewPager 内的子视图中,这本质上是 Fragment 的布局。如果您的 Fragment 的列表布局非常简单,您甚至不需要使用 NestedScrollView。

示例布局

Activity的布局:

<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="@dimen/app_bar_height"
        android:fitsSystemWindows="true"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed|snap">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:minHeight="@dimen/app_bar_height"
                android:scaleType="centerCrop"
                android:src="@drawable/my_bg"
                app:layout_collapseMode="parallax"
                app:layout_collapseParallaxMultiplier="0.8"
                app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed" />

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/AppTheme.PopupOverlay" />

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

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <android.support.design.widget.TabLayout
            android:id="@+id/content_tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <android.support.v4.view.ViewPager
            android:id="@+id/viewPager"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />
    </LinearLayout>

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

如果您不需要 TabLayout,您可以放弃 LinearLayout 并使 ViewPager 独立。但一定要在 ViewPager 属性中使用app:layout_behavior="@string/appbar_scrolling_view_behavior"

简单片段的布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

复杂片段的布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillViewport="true"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Hello World"/>
    </RelativeLayout>
</android.support.v4.widget.NestedScrollView>

示例应用:CollapsingToolbarPoc

【讨论】:

  • 这是为我做的。谢谢!
  • 嘿 Ino,这似乎有效,但是当我在 TabLayout 顶部添加另一个视图时,它就坏了。你能帮忙吗?
  • 你救了我的命。 :)
【解决方案5】:

有同样的问题。

将 NestedScrollView 围绕 ViewPager 时,它不起作用。

DID 的作用是将 NestedScrollView 放入我正在 ViewPager 中加载的片段的布局。



<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="fill_parent"
        android:layout_height="fill_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@id/fragment_detail"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="fill_vertical"
            android:orientation="vertical">
    ...Content...
    </LinearLayout>
</android.support.v4.widget.NestedScrollView>

【讨论】:

  • 如果内容是带有另一个 NestedScrollView 的协调器布局,它是否有效?并且父 CoordinatorLayout 是否监听那里的滚动事件?
  • 这对我有用。我将 NestedScrollView 添加到我的 ViewPager 而不是 ViewPager 的每个项目中。谢谢。
  • 我在片段中有 swipefreshlayout,根据你的解决方案,我将 NestedScrollview 放在片段中,在 swipeRefreshLayout 上方.. 然后没有加载数据。没用。
【解决方案6】:

你可以这样尝试,viewpager 工作正常,但是 viewpager 的高度是固定的。

我仍在寻找更好的解决方案......所以请告诉我任何可以做得更好的方法,谢谢

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

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

        <android.support.v4.view.ViewPager
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"/>
    </LinearLayout>
</android.support.v4.widget.NestedScrollView>

【讨论】:

  • 您是否尝试将其封装成“android.support.design.widget.CoordinatorLayout”和折叠工具栏?
  • 您的解决方案似乎证明问题出在高度。发现任何改进了吗@Paul?
  • 这个对我有用,但我还必须在嵌套视图上调用 setFillViewport(true)。
【解决方案7】:

我遇到了同样的问题,只是通过一些更改来修复它。 ViewPagerNestedScrollView 中不起作用。只需将您的ViewPager 作为您的CoordinatorLayout 的直接子代即可。那么每个 ViewPager 的 Fragment 的内容应该包含在NestedScrollView 中。就是这样。

【讨论】:

  • 即使内容是带有另一个 NestedScrollView 的协调器布局?
  • 片段列表视图数据不是这样加载的!
【解决方案8】:

只需将这一行放在 NestedScrollView 中

android:fillViewport="true"

【讨论】:

  • 非常感谢。如果我想通过直接触摸 CollapsingToolbarLayout 来滚动怎么办?
  • 将“app:layout_behavior="@string/appbar_scrolling_view_behavior"”添加到您的 NestedScrollView
【解决方案9】:

您不需要使用 NestedScrollView 来获得视差效果。 试试这样的:

<android.support.design.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:animateLayoutChanges="true">

        <android.support.v7.widget.Toolbar
            android:id="@+id/my_toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_scrollFlags="scroll|enterAlways"/>

        <android.support.design.widget.TabLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/my_tab_layout"
            android:fillViewport="false"
            app:tabMode="fixed"/>
     </android.support.design.widget.AppBarLayout>

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

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

【讨论】:

  • 只要你所有的ViewPager片段都是以太RecyclerViewNestedScrollView,这将起作用。如果不是(例如ListView),那么 Lollipop 及更高版本的解决方法是调用listView.setNestedScrollingEnabled(true)
  • 终于可以在不添加额外滚动容器的情况下使用了!顺便说一句,您也可以在 xml 中设置此 setNestedScrollingEnabled(true):android:nestedScrollingEnabled="true"
  • 如果我运行我的应用程序,在一个选项卡中折叠工具栏,切换选项卡,然后将工具栏拉回,它是空的。必须在它处于非折叠状态时切换选项卡才能再次将其恢复。这么近,却这么远:(
【解决方案10】:

我有一个布局,其中有一个应用程序工具栏,其下方是 NestedScrollView,然后嵌套工具栏内是一个 LinearLayout,LinearLayout 下方是一个视图寻呼机。想法是 NestedScrollView 内的 LinearLayout 是固定的 - 您可以在视图寻呼机的视图之间向左/向右滑动,但此内容不会移动,至少 水平。由于嵌套的滚动视图,您应该仍然能够垂直滚动所有内容。这就是想法。所以我将嵌套的 NestedScrollView 高度设置为 match_parent,填充视口,然后将所有子布局/ViewPager 设置为 wrap_content。

在我看来这是对的。但它不起作用 - ViewPager 允许我向左/向右移动,但不允许垂直滚动,无论 viewpager 页面内容是否推到当前屏幕底部下方。原来这基本上是因为 ViewPager 不尊重 wrap_content 跨其各个页面。

我通过子类化 ViewPager 来解决这个问题,让它根据当前选定的项目改变高度,有点强迫它表现得像 layout_height="wrap_content":

public class ContentWrappingViewPager extends ViewPager {

    public ContentWrappingViewPager(Context context) {
        super(context);
    }

    public ContentWrappingViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        int height = 0;
        if (getChildCount() > getCurrentItem()) {
            View child = getChildAt(getCurrentItem());
            child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
            int h = child.getMeasuredHeight();
            if(h > height) height = h;
        }

        heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}

解决方案的灵感来自this answer。为我工作!

【讨论】:

  • 但是滚动视图会自动滚动到顶部?如何解决这个问题?
  • 谢谢。你救了我。
【解决方案11】:

使用以下自定义类来解决您的问题。 不要忘记在 pagechangelistener 上调用 onRefresh() 方法。

public class ContentWrappingViewPager extends ViewPager
{
    private int width = 0;
    public ContentWrappingViewPager(Context context) {
        super(context);
    }

    public ContentWrappingViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        int height = 0;
        width = widthMeasureSpec;
        if (getChildCount() > getCurrentItem()) {
            View child = getChildAt(getCurrentItem());
            child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
            int h = child.getMeasuredHeight();
            if(h > height) height = h;
        }

        heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    public void onRefresh()
    {
        try {
            int height = 0;
            if (getChildCount() > getCurrentItem()) {
                View child = getChildAt(getCurrentItem());
                child.measure(width, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
                int h = child.getMeasuredHeight();
                if(h > height) height = h;
            }

            int heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
            ViewGroup.LayoutParams layoutParams = this.getLayoutParams();
            layoutParams.height = heightMeasureSpec;
            this.setLayoutParams(layoutParams);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

【讨论】:

    【解决方案12】:

    我从主布局中删除了 NestedScrollView 并作为 Parent 插入到我所有应该在 中加载的 Fragment 布局中ViewPager 为我解决了这个问题。

    【讨论】:

      【解决方案13】:

      我遇到了类似的问题(但没有CollapsingToolbarLayout,只是AppBarLayout 中的一个简单的Toolbar + TabLayout)。在NestedScrollView 中向下滚动ViewPager 的内容时,我希望工具栏滚动到视线之外。

      我的布局是这样的:

      <android.support.design.widget.CoordinatorLayout
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              tools:context=".MainActivity">
      
              <android.support.design.widget.AppBarLayout
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:animateLayoutChanges="true">
      
                  <android.support.v7.widget.Toolbar
                      android:id="@+id/my_toolbar"
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      app:layout_scrollFlags="scroll|enterAlways"/>
      
                  <android.support.design.widget.TabLayout
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:id="@+id/my_tab_layout"
                      android:fillViewport="false"
                      app:tabMode="fixed"/>
               </android.support.design.widget.AppBarLayout>
      
               <android.support.v4.widget.NestedScrollView
                      android:id="@+id/container"
                      android:layout_width="match_parent"
                      android:layout_height="match_parent"
                      android:fillViewport="true"
                      android:layout_gravity="fill_vertical"
                      app:layout_behavior="@string/appbar_scrolling_view_behavior">
      
                      <android.support.v4.view.ViewPager
                          android:id="@+id/my_view_pager"
                          android:layout_width="match_parent"
                          android:layout_height="match_parent"
                           app:layout_behavior="@string/appbar_scrolling_view_behavior">
                      </android.support.v4.view.ViewPager>
               </android.support.v4.widget.NestedScrollView>
      
      </android.support.design.widget.CoordinatorLayout>
      

      这个布局没有按预期工作,但我通过简单地去掉NestedScrollView 并改用LinearLayout 来解决它。它在 Android 支持库 v23.1.0 上立即为我工作。以下是布局的最终​​结果:

      <android.support.design.widget.CoordinatorLayout
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              tools:context=".MainActivity">
      
              <android.support.design.widget.AppBarLayout
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:animateLayoutChanges="true">
      
                  <android.support.v7.widget.Toolbar
                      android:id="@+id/my_toolbar"
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      app:layout_scrollFlags="scroll|enterAlways"/>
      
                  <android.support.design.widget.TabLayout
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:id="@+id/my_tab_layout"
                      android:fillViewport="false"
                      app:tabMode="fixed"/>
               </android.support.design.widget.AppBarLayout>
      
               <LinearLayout
                  android:orientation="vertical"
                  android:id="@+id/container"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:layout_marginBottom="4dp"
                  app:layout_behavior="@string/appbar_scrolling_view_behavior">
      
                      <android.support.v4.view.ViewPager
                          android:id="@+id/my_view_pager"
                          android:layout_width="match_parent"
                          android:layout_height="match_parent"
                           app:layout_behavior="@string/appbar_scrolling_view_behavior">
                      </android.support.v4.view.ViewPager>
               </LinearLayout>
      
      </android.support.design.widget.CoordinatorLayout>
      

      PS:这实际上是我项目中的两个布局文件。我在此处复制并粘贴了它们,并尝试将它们构造成单个文件中的样子。如果我在复制粘贴时搞砸了,我很抱歉,但如果是这种情况,请告诉我,以便我修复它。

      【讨论】:

        【解决方案14】:

        只需将 android:fillViewport="true" 添加到 NestedScrollView xml 标记

        <androidx.core.widget.NestedScrollView
               android:layout_width="match_parent"
               android:layout_height="match_parent"
               android:fillViewport="true"
               app:layout_behavior="@string/appbar_scrolling_view_behavior"
               >
               <androidx.viewpager.widget.ViewPager
                   android:id="@+id/viewpager"
                   android:layout_width="match_parent"
                   android:layout_height="match_parent"/>
           </androidx.core.widget.NestedScrollView>
        

        【讨论】:

          【解决方案15】:

          以下内容应确保视图分页器的正确高度。该片段是视图分页器提供的第一个片段。

          <android.support.v4.widget.NestedScrollView
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              app:layout_behavior="@string/appbar_scrolling_view_behavior">
          
              <LinearLayout
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:orientation="horizontal">
          
                  <android.support.v4.view.ViewPager
                      android:id="@+id/viewPager"
                      android:layout_width="match_parent"
                      android:layout_height="match_parent"/>
          
                  <fragment
                      class="com.mydomain.MyViewPagerFragment"
                      android:id="@+id/myFragment"
                      android:layout_width="match_parent"
                      android:layout_height="match_parent"
                      android:visibility="gone"/>
          
              </LinearLayout>
          
          </android.support.v4.widget.NestedScrollView>
          

          【讨论】:

            【解决方案16】:

            我也有同样的愿望,将ViewPager 嵌套在NestedScrollView 中。但这对我也不起作用。就我而言,ViewPager 位于Fragment 内,因此我可以根据与抽屉的交互来切换内容。当然,AppBar、collapse 等以及支持库的所有新功能都必须工作。

            如果用户在分页器中垂直滚动页面,我希望其他页面也滚动相同,以给用户一个分页器本身滚动的整体印象。

            我所做的,有效的是,我不再将 ViewPager 嵌入到 NestedScrollView 中,而是将包含的片段的内容嵌入到 NestedScrollView 中。

            然后,如果一个片段中有滚动,我会通知容器新的滚动位置,它会存储它。

            最后,在从寻呼机检测到向左或向右滑动时(使用addOnPageChangeListener 寻找拖动状态),我通知目标向左或向右片段它必须滚动的位置(基于它的容器知识)以对齐我来自的片段。

            【讨论】:

            • 所以本质上是您编写了自己的 ViewPager 实现?你能分享这方面的代码吗?
            • 我做了类似的事情(如果我理解正确的话),这是在您的协调器布局中进行线性布局,其中包含 tablayout(如果需要)和 viewpager,而不是嵌套滚动视图。然后片段本身将嵌套的滚动视图作为它们的根元素。这对我有用,但我更喜欢只有一个嵌套滚动视图,而不是在每个片段中重复它。
            【解决方案17】:

            我知道一种可行的解决方案:您使用 Activity、CoordinatorLayout 并使用 FrameLayout、容器。然后。使用片段管理器将片段放入容器中。例如我的代码:

            <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:orientation="vertical">
            
            <android.support.design.widget.AppBarLayout
                android:id="@+id/appbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:elevation="0dp">
            
                <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    app:layout_scrollFlags="scroll|enterAlways"
                    android:layout_height="@dimen/toolbar_height"
                    android:background="?attr/colorPrimary"
                    android:gravity="center">
            
                </android.support.v7.widget.Toolbar>
            
            
            </android.support.design.widget.AppBarLayout>
            
            <FrameLayout
                android:id="@+id/container"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                app:layout_behavior="@string/appbar_scrolling_view_behavior">
            
            </FrameLayout>
            

            和片段:

            <LinearLayout 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:orientation="vertical"
            tools:context="com.improvemdia.inmyroom.MainFragment">
            
            <android.support.v4.view.ViewPager
                android:id="@+id/pager"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
            

            然后使用 FragmentManager

            getSupportFragmentManager().beginTransaction()
                        .replace(R.id.container, new MainFragment())
                        .commit();
            

            【讨论】:

            • 我认为片段布局中的LinearLayout 在这里是不必要的。请参阅我的回答,我在 Activity 的布局中使用了 LinearLayout 而不是 FrameLayout,并且我的片段的根是 ViewPager(无包装布局)。
            【解决方案18】:

            花了几个小时尝试了上述所有建议后,我终于成功了。关键是将NestedScrollView 放入PageViewer,并将layout_behavior 设置为'@string/appbar_scrolling_view_behavior' 以获得BOTH 视图。最终的布局是这样的

            <CoordinatorLayout>
                <ViewPager app:layout_behavior="@string/appbar_scrolling_view_behavior">
                    <NestedScrollView fillViewport="true" app:layout_behavior="@string/appbar_scrolling_view_behavior">
                        <Content>...</Content>
                    </NestedScrollView>
                </ViewPager>
                <AppBarLayout>...</AppBarLayout>
            </CoordinatorLayout>
            

            【讨论】:

              【解决方案19】:

              您刚刚删除了 xml 中的 NestedScrollView 并将此代码添加到您的类中

              yourGridView.setNestedScrollingEnabled(true);

              【讨论】:

                【解决方案20】:

                我尝试了很多方法,但没有奏效。 我提到了this,我的问题就解决了。

                【讨论】:

                  【解决方案21】:

                  实际上 NestedScrollView 不需要是 CoordinatorLayout 中 CollapsingToolbarLayout 的直接兄弟。我取得了一些非常奇怪的成就。 appbar_scrolling_view_behavior 在 2 个嵌套片段中工作。

                  我的活动布局:

                  <android.support.design.widget.CoordinatorLayout>
                  
                      <android.support.design.widget.AppBarLayout>
                  
                              <android.support.design.widget.CollapsingToolbarLayout>
                  
                                  <include
                                      layout="@layout/toolbar"/>
                  
                              </android.support.design.widget.CollapsingToolbarLayout>
                  
                      </android.support.design.widget.AppBarLayout>
                  
                      <FrameLayout
                          android:id="@+id/container"
                          app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
                  
                  </android.support.design.widget.CoordinatorLayout>
                  

                  在“容器”frameLayout 中,我对这个片段进行了膨胀:

                  <android.support.constraint.ConstraintLayout>
                  
                      ... some content ...
                  
                      <android.support.v4.view.ViewPager/>
                  
                      ...
                  
                  </android.support.constraint.ConstraintLayout>
                  

                  ViewPager 中的片段如下所示:

                  <RelativeLayout>
                  
                      <android.support.v7.widget.RecyclerView/>
                  
                      ....
                  
                  </RelativeLayout>
                  

                  NestedScrollView 在 CoordinatorLayout 子层次结构中如此深入,滚动行为仍然有效,这让我感到惊讶。

                  【讨论】:

                    猜你喜欢
                    • 2018-05-14
                    • 1970-01-01
                    • 2020-04-28
                    • 1970-01-01
                    • 2021-03-10
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    相关资源
                    最近更新 更多