【问题标题】:ScrollView inside ViewPager inside NestedScrollView not scrollingNestedScrollView 内的 ViewPager 内的 ScrollView 不滚动
【发布时间】:2019-10-16 13:11:04
【问题描述】:

我有一个包含嵌套 ScrollView 的活动。在这个 NestedScrollView 里面有一个 ViewPager。每个页面都有一个包含 ScrollView 的视图。这个最里面的滚动视图根本不滚动。 如何在这个最里面的 ScrollView 中启用滚动行为

这是Activity的xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".MainActivity">

    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">

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

            <androidx.viewpager.widget.ViewPager
                android:id="@+id/pager"
                android:layout_width="match_parent"
                android:layout_height="300dp" />

            <ImageView
                android:layout_width="400dp"
                android:layout_height="400dp"
                android:layout_marginTop="10dp"
                android:src="@drawable/ic_launcher_background" />

            <ImageView
                android:layout_width="400dp"
                android:layout_height="400dp"
                android:layout_marginTop="10dp"
                android:src="@drawable/ic_launcher_background" />

            <ImageView
                android:layout_width="400dp"
                android:layout_height="400dp"
                android:layout_marginTop="10dp"
                android:src="@drawable/ic_launcher_background" />

            <ImageView
                android:layout_width="400dp"
                android:layout_height="400dp"
                android:layout_marginTop="10dp"
                android:src="@drawable/ic_launcher_background" />
        </LinearLayout>
    </androidx.core.widget.NestedScrollView>

</androidx.constraintlayout.widget.ConstraintLayout>

这是视图寻呼机的xml

    <?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    android:orientation="vertical">
    <ImageView
        android:id="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="800dp" />

</androidx.core.widget.NestedScrollView>

【问题讨论】:

  • 我不明白为什么你有一个 NestedScrollView 包装你的 ViewPager?由于您在每个视图上都有 ScrollView,我认为不需要这样做。如果有一些代码也很好。
  • 因为我的视图寻呼机是 Activity 的一部分,而 Activity 包含许多不同类型的小视图,我需要它们与视图寻呼机内容一起滚动
  • 找到解决方案了吗?

标签: android scrollview android-nestedscrollview


【解决方案1】:

在您的NestedScrollView 中添加android:fillViewport="true"。希望问题能解决

【讨论】:

  • 请分享您的 xml。我想看看你在做什么
  • 共享 xml。请看一下
  • 使用根布局作为 androidx.constraintlayout.widget.ConstraintLayout 如果我们使用 androidx.core.widget.NestedScrollView 和 androidx.viewpager.widget.ViewPager 即 androidx 版本的 NestedScrollView 和 ViewPager 并且您的视图寻呼机是不是。在 NestedScrollView 内滚动,Make。根布局为 ConstraintLayout,它将起作用
【解决方案2】:

只有当里面有内容并且它占据了视图的所有宽度时,滚动视图才会滚动。然后你就可以滚动了

【讨论】:

  • 它包含所有滚动内容
  • 父容器的布局高度和宽度是多少?他们可能是因为没有内容而崩溃了。
【解决方案3】:

如果在这里添加xml文件会容易得多 还, 尝试在 viewPager 持有的 Views 中添加 Activity 中的 Scroll View 和 Nested Scroll View 也尝试添加

fillViewPorty = true

在您的顶部滚动视图中

【讨论】:

  • 请分享 XML
  • 分享请看一下
  • @vs.thaakur 使用 constraintLayout 有什么特别的原因吗?
  • 是的,我的原始视图层次结构非常复杂。这是一个简化的案例
  • 尝试在 constrainLayout 标签之后使用 标签...而不是
猜你喜欢
  • 1970-01-01
  • 2019-12-23
  • 2021-01-01
  • 2023-04-08
  • 2016-10-20
  • 1970-01-01
  • 2020-04-28
  • 2012-09-22
  • 1970-01-01
相关资源
最近更新 更多