NestedScrollView中的ViewPager中的ScrollView无法滚动。

3

我有一个包含嵌套ScrollView的活动。在这个NestedScrollView中,有一个ViewPager。每一页都有一个包含ScrollView的视图。但是,最内层的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>

这是ViewPager的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>

我不明白为什么你要在ViewPager外面套一个NestedScrollView?因为每个视图上都有ScrollView,所以我认为这是不必要的。同时,提供一些代码会更好。 - ArbenMaloku
因为我的视图页面是 Activity 的一部分,而且 activity 包含了许多不同种类的小视图,我需要它们随着视图页面的内容一起滚动。 - vs.thaakur
找到解决方案了吗? - nicover
3个回答

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

请分享你的XML文件。我想看看你实际上在做什么。 - Tariqul Islam
共享 XML。请查看。 - vs.thaakur
请使用 androidx.constraintlayout.widget.ConstraintLayout 作为根布局。 如果我们正在使用 androidx.core.widget.NestedScrollView 和 androidx.viewpager.widget.ViewPager,即 NestedScrollView 和 ViewPager 的 androidx 版本,并且您的 ViewPager 在 NestedScrollView 中无法滚动,请将根布局设置为 ConstraintLayout,这样就可以解决问题了。 - Yash Bhardwaj

0

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


它包含了所有滚动的内容。 - vs.thaakur
父容器的布局高度和宽度是多少?有可能由于没有内容而导致它们折叠了。 - d12

0
如果您在此处添加xml文件,将会更加容易。 另外, 尝试在Activity中添加ScrollView,在被ViewPager持有的视图中添加NestedScrollView 还可以尝试添加

fillViewPorty = true

在您的顶部滚动视图中


请分享XML。 - Humxa Moghal
请查看共享内容。 - vs.thaakur
@vs.thaakur 有使用 ConstraintLayout 的特殊原因吗? - Humxa Moghal
是的,我的原始视图层次结构非常复杂。这只是一个简化的情况。 - vs.thaakur
尝试在 constrainLayout 标签后面使用 <ScrollView> 标签,而不是 <Nested>。 - Humxa Moghal
让我们在聊天中继续这个讨论 - Humxa Moghal

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接