android - 无法将 ViewPager 放入 ScrollVIew/NestedScrollView

标签 android android-layout android-viewpager scrollview

我有一个 ViewPager,它位于父线性布局内,ViewPager 下方有一些按钮。 ViewPager 一直运行良好,直到最近我将父级从 LinearLayout 更改为 ScrollView。现在 ViewPager 消失了。请不要将其标记为垃圾邮件或重复。

此外,Playstore 上目前有大量应用程序正在使用它。查看此应用程序 - link

我的尝试:

  1. setFillViewport (true) - 这对我不起作用。
  2. 将 layout_height 设置为 0dp
  3. 使用 CustomScrollView

所有这些都对我没有帮助。我在 Android M 上运行该应用。

这是我的 layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<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:fillViewport="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/black"
tools:context=".activities.HomeActivity">

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

<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:onClick="pagerClick"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_gravity="top"
android:layout_weight="1"
android:overScrollMode="never"/>

<RelativeLayout
android:layout_weight="0.8"
android:layout_marginTop="30dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
.....two buttons
</RelativeLayout>

</LinearLayout>

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

问题:

我怎样才能做到这一点?另外,有官方的方法吗?

最佳答案

我通过固定 ViewPager 的高度解决了这个问题:

<?xml version="1.0" encoding="utf-8"?>
<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:fillViewport="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/black"
tools:context=".activities.HomeActivity">

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

<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:onClick="pagerClick"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_gravity="top"/>

...

</LinearLayout>

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

关于android - 无法将 ViewPager 放入 ScrollVIew/NestedScrollView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46138015/

相关文章:

android - 如何将指南针添加到 map View

android - 在 Android 的任何布局中使用 colors.xml

android - 如何在 Viewpager 中禁用预加载下一页?

android - 我应该使用哪个资源限定符来支持 1080p、720p 安卓电视? - 安卓

android - Python 等效于 Java DataInputStream.readUTF()

Android:如何在图像之上实现绘图

java - Android 4 标签布局的最佳方式?

android - Gridview——点击图片在Viewpager中查看图片

android - 我想在 fragment 中创建图像幻灯片 View ?

android - 从处理程序启动新 Activity