【问题标题】:How to place Listview and Viewpager inside a ScrollView?如何在 ScrollView 中放置 Listview 和 Viewpager?
【发布时间】:2026-02-10 03:30:01
【问题描述】:

我有一个 listview 和 viewpager,用于生成我的图像滑块。这两个有自己的适配器。在我的 layout.xml 中,我想将它们放入滚动视图中,但是当我这样做时,viewpager 不会使用 listview 滚动。如果您有想法,请帮帮我吗?

谢谢

 <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context="com.test.MainActivity">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:id="@+id/test">

            <android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/pager_news"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1" />

            <ListView
                android:id="@+id/news_lw"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="2"
                android:nestedScrollingEnabled="true">

            </ListView>
        </LinearLayout>

    </LinearLayout>

【问题讨论】:

  • 将 NestedScrollView 与协调器布局一起使用

标签: android listview android-viewpager scrollview


【解决方案1】:

NestedScrollViewRecyclerView 一起使用,而不是ListView

试试这个:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/test">

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

        <android.support.v7.widget.RecyclerView
            android:id="@+id/news_lw"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@id/pager_news">

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

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

【讨论】:

  • 这只是一个例子。你也可以使用 LinearLayout。
  • 很高兴为您提供帮助:)
【解决方案2】:

ListView 不支持嵌套滚动。因此,如果您认为这种行为是必要的,请改用 NestedScrollViewRecyclerView

更好的是使用CoordinatorLayout 并定义一个自定义行为,如果RecyclerView 滚动特定扩展,则该行为将ViewPager 滚动或滚动。

【讨论】: