【问题标题】:Coordinator layout with viewpager, scroll doesn't work带有 viewpager 的协调器布局,滚动不起作用
【发布时间】:2017-04-18 15:55:40
【问题描述】:

我正在这个环境中工作,

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 的视图。这是布局文件:

<?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>

我的问题是滚动不适用于视图。我看到一些实现 onTouch 方法来模拟滚动的帖子,但我想使用 coordinatorLayout。 谢谢。

【问题讨论】:

  • 这里需要协调哪些布局?换句话说,例如,LinearLayout 有什么问题?
  • 您要这样做吗? gist.github.com/iPaulPro/…您缺少 CollapsingToolbarLayout
  • 是的,我正在尝试这样做,但我拥有的寻呼机不包含 recyclerview,它包含包含组件的 LinearLayout。
  • 好的,所以你有一个不滚动的 LinearLayout?为什么这是一个问题? LinearLayout 不滚动。不管是 ViewPager。你需要一个滚动视图

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


【解决方案1】:

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

【讨论】:

    【解决方案2】:
    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" >
        
        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager"
            android:layout_width="match_parent"
            android:layout_height="200dp"/>
    
    <!--Wrap content will not work here you have to give fix height-->
    
    </androidx.core.widget.NestedScrollView>
    

    【讨论】:

    • 你应该将 android:fillViewport="true" 添加到 NestedScrollView 标签。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-14
    • 2019-01-31
    • 2018-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多