【问题标题】:ViewPager in CoordinatorLayout then scroll tablayout offscreen not workingCoordinatorLayout 中的 ViewPager 然后在屏幕外滚动 tablayout 不起作用
【发布时间】:2015-10-04 05:53:12
【问题描述】:

我在 CoordinatorLayout 中有一个 viewpager,如下所示:

    <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/AppTheme"/>

    <android.support.design.widget.TabLayout
        android:id="@+id/tab_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMode="scrollable"/>
</android.support.design.widget.AppBarLayout>

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

在 viewpager 中,每个片段都是一个 webview。

代码可以运行,但是当我滚动我的 webview 时。屏幕外的 tablayout 不起作用。它就在标题处。

更新:

我尝试使用 relativelayout 来满足我的视图并使用 NestedScrollView 作为 relativelayout 的父级,但 webview 消失了(进度条显示正常)。是bug???

这是我的片段布局:

<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/nestedScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:minHeight="300dp">

    <ProgressBar
        android:id="@+id/pb"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="fill_parent"
        android:layout_height="2dp"
        android:indeterminateOnly="false"
        android:max="100"
        android:progressDrawable="@drawable/progress_bar_states"></ProgressBar>

    <com.handmark.pulltorefresh.library.PullToRefreshWebView
        android:id="@+id/webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/pb" />
</RelativeLayout>

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

【问题讨论】:

    标签: android android-coordinatorlayout androiddesignsupport


    【解决方案1】:

    试试这样:

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/home_coordinator_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <android.support.design.widget.AppBarLayout
            android:id="@+id/home_appbar_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:elevation="2dp"
            app:paddingEnd="0dp"
            app:paddingStart="0dp">
    
            <include layout="@layout/toolbar_appcompat"></include>
    
            <android.support.design.widget.TabLayout
                android:id="@+id/home_tab_layout"
                style="@style/TabLayoutStyle"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:tabContentStart="2dp"
                app:tabGravity="fill"
                app:tabIndicatorColor="@android:color/white"
                app:tabIndicatorHeight="2dp"
                app:tabMinWidth="24dp"
                app:tabMode="fixed"
                app:tabPadding="1dp"
                app:tabSelectedTextColor="@android:color/tab_indicator_text"
                app:tabTextColor="@android:color/darker_gray" />
        </android.support.design.widget.AppBarLayout>
    
        <android.support.v4.view.ViewPager
            android:id="@+id/home_view_pager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            app:paddingEnd="0dp"
            app:paddingStart="0dp" />
    
        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab_home"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="20dp"
            android:src="@drawable/arrow_toggle"
            app:borderWidth="1dp"
            app:elevation="3dp"
            app:fabSize="normal"
            app:layout_anchor="@+id/home_coordinator_layout"
            app:layout_anchorGravity="bottom|right|end"
            app:pressedTranslationZ="2dp"
            app:rippleColor="@color/swipe_refresh_color_scheme_green" />
    
    </android.support.design.widget.CoordinatorLayout>
    

    它在我的项目中运行良好。

    【讨论】:

    • 您的浏览器中有哪些内容?我的是一个Relativelayout,里面有一个进度条和webview。我检查了 viewpager 中的 NestedScrollView 是否。它工作正常。
    • 只是页面中的RecyclerView
    • RecyclerView 或 NestedScrollView 会正常工作。我尝试将Relativelayout放入NestedScrollView但Webview无法显示,我也发现其他人也有同样的问题。
    • 对不起,我没试过WebView
    • @Swifty。抱歉,我的要求是我需要显示两个选项卡(webview 1 和 webview2)所以我使用 viewpager 来加载片段。我只是将 Webview 放在嵌套的滚动视图中并使用 viewpager 适配器进行渲染。 layout codewebview adapter code
    【解决方案2】:

    对于那些在使用 NestedScrollView 时内容不显示的问题,将“fillViewport”添加到 xml:

    <android.support.v4.widget.NestedScrollView
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/nestedScrollView"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:fillViewport="true">
    

    【讨论】:

      【解决方案3】:

      问题可能出在 app:layout_behavior="@string/appbar_scrolling_view_behavior" 上。 您可以在这里找到更多信息:Android - footer scrolls off screen when used in CoordinatorLayout

      【讨论】:

        猜你喜欢
        • 2015-09-04
        • 1970-01-01
        • 2018-04-23
        • 1970-01-01
        • 2015-08-26
        • 2016-09-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多