【问题标题】:How make put ViewPager inside ScrollView and scroll all view as one part?如何将 ViewPager 放入 ScrollView 并将所有视图作为一个部分滚动?
【发布时间】:2019-03-01 00:48:19
【问题描述】:


我有一个,在 里面有,就像下面这样

            <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent">

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


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

                    <!--someViews-->

                </LinearLayout>


                <!--then viewPager-->
                <androidx.viewpager.widget.ViewPager
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />

            </LinearLayout>

        </ScrollView>

然后视图寻呼机包含一些,它们也有一个高视图。 问题是滚动视图不滚动,因为视图不需要滚动,但 viewpager 片段内的视图不滚动 那么,是否可以将包括内部视图寻呼机在内的整个视图作为一个视图滚动? 我的意思是我不需要将 viewpager 片段作为单独的部分滚动,我需要将它作为活动视图的一部分滚动。

谢谢

【问题讨论】:

    标签: activity viewpager scrollview fragments android android-viewpager scrollview


    【解决方案1】:

    我相信您正在寻找的设置将在 CoordinatorLayout 中使用 NestedScrollView 小部件,如下所示:

    <?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"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent">
    
      <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="16dp">
    
        <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical">
    
          <TextView
            android:id="@+id/title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/app_name"
            android:textAppearance="@style/Base.TextAppearance.AppCompat.Display1" />
    
          <TextView
            android:id="@+id/subTitle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/subtitle"
            android:textAppearance="@style/Base.TextAppearance.AppCompat.Body1" />
    
          <androidx.viewpager.widget.ViewPager
            android:id="@+id/viewPager"
            android:layout_width="match_parent"
            android:layout_height="300dp" />
        </LinearLayout>
    
      </androidx.core.widget.NestedScrollView>
    </androidx.coordinatorlayout.widget.CoordinatorLayout>
    

    我刚刚使用片段中的单个文本视图的示例项目对此进行了测试,并且我能够滚动视图和滑动浏览页面。

    请注意:我确实必须为 ViewPager 提供特定高度才能显示。

    祝你好运,编码快乐!

    【讨论】:

    • 是的,当我在 viewpager 上放一个高电平时它起作用了,但我不需要这样做,因为我不知道 viewpager 内的片段足够高
    • @Tefa 如果是这种情况,那么 ViewPager 不是要使用的正确小部件。考虑使用带有水平 LayoutManager 的 RecyclerView,但 NestedScrollView 仍然适用。
    • 我已经删除了recycler-view并使用了FrameLayout并替换了可能的片段
    • 这很好,但为什么我们必须使用固定高度?因为不推荐硬编码。还有其他方法吗?谢谢。
    • @MehmetGür 我相信 NestedScrollView 包装了它的内容,所以尽管在布局中说匹配父级,但它不知道自己要画多大,所以需要调整子级的大小
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-15
    相关资源
    最近更新 更多