我有一个 ViewPager,它位于父线性布局内,ViewPager 下方有一些按钮。 ViewPager 一直运行良好,直到最近我将父级从 LinearLayout 更改为 ScrollView。现在 ViewPager 消失了。请不要将其标记为垃圾邮件或重复。
此外,Playstore 上目前有大量应用程序正在使用它。查看此应用程序 - link
我的尝试:
- setFillViewport (true) - 这对我不起作用。
- 将 layout_height 设置为 0dp
- 使用 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/