android - 带有 viewpager 的协调器布局,滚动不起作用

标签 android android-layout android-viewpager android-coordinatorlayout android-scroll

我在这个环境下工作,

compileSdkVersion 25
    buildToolsVersion '25.0.0'
    defaultConfig {
        minSdkVersion 21
        targetSdkVersion 25

具有这些依赖项:

compile 'com.android.support:appcompat-v7:25.0.1'
    compile 'com.android.support:design:25.0.1'
    compile 'com.android.support:recyclerview-v7:25.0.1'
    compile 'com.android.support:cardview-v7:25.0.1'

我想创建一个带有 coordinatorlayout 但包含 viewPager 而不是 recyclerview 的 View 。这是布局文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="72dp"
            app:tabGravity="fill"
            app:tabMode="fixed" />
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</android.support.design.widget.CoordinatorLayout>

我的问题是滚动对 View 不起作用。我看到一些帖子实现了 onTouch 方法来模拟滚动,但我想使用 coordinatorLayout。 谢谢。

最佳答案

要使用 CoordinatorLayout,它需要一个在 ViewPager 中实现 NestedScrollingChild 接口(interface)的 View ,例如 NestedScrollView。因此,将您的 LinearLayout 嵌套为 NestedScrollView 的子项。

关于android - 带有 viewpager 的协调器布局,滚动不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40954222/

相关文章:

java - 从 Activity 调用 Fragment 的适配器时出现 NullPointerException

android - 在 ViewPager 中水平滚动 WebView

android - OpenCV Android Kmeans 无法按预期工作

android - 为gradle传递的参数为null

android - 第二次调用 setAdapter() 后 ViewPager 不刷新 View

java - 海量数据android的tablelayout

Android - 从通知操作按钮调用方法

java - 快速写入大文本文件

java - 在android/JAVA中改变变量的值

android - 通过多行 TextView 删除