【问题标题】:Nested RecyclerView inside ViewPager inside Fragment not affecting CoordinatorLayout在 Fragment 内的 ViewPager 内嵌套 RecyclerView 不影响 CoordinatorLayout
【发布时间】:2016-03-07 12:10:55
【问题描述】:

我有一个带有 coordinatorlayout 的活动,里面有一个 FrameLayout。 Framelayout 内部是一个 Viewpager,而 Viewpager 中的 Fragment 之一包含一个 Recyclerview。是否有可能让 RecyclerView 影响最顶层的 CoordinatorLayout?

谢谢。

main.xml(活动xml布局)

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer_layout"
    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:fitsSystemWindows="true"
    xmlns:ads="http://schemas.android.com/apk/res-auto">
    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/coordinator"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
            <android.support.design.widget.AppBarLayout
                android:layout_height="wrap_content"
                android:layout_width="match_parent">

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


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

            <!-- This is the container to all other fragments. -->
            <!-- The only other ones are in the view pager. -->
            <FrameLayout
                android:id="@+id/content_frame"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:background="?attr/colorPrimaryDark"
                android:layout_weight="1"
                app:layout_behavior="@string/appbar_scrolling_view_behavior"
                android:visibility="visible"/>

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

    <!-- Side Menu -->
    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/drawer_header"
        app:menu="@menu/drawer"/>
</android.support.v4.widget.DrawerLayout>

活动的根片段

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    android:layout_height="match_parent">

    <!--<android.support.design.widget.AppBarLayout-->
        <!--android:layout_height="wrap_content"-->
        <!--android:layout_width="match_parent">-->
        <android.support.design.widget.TabLayout
            android:id="@+id/tablayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?attr/colorPrimary"
            app:tabGravity="fill"
            android:theme="@style/ThemeOverlay.AppCompat.Dark"
            app:layout_scrollFlags="scroll|enterAlways"
            android:visibility="visible"/>
        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            android:background="?attr/colorPrimaryDark"
            android:visibility="visible"/>
    <!--</android.support.design.widget.AppBarLayout>-->
</LinearLayout>

Viewpager 中的片段

【问题讨论】:

  • schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> schemas.android.com/apk/res/android" android:id="@+id/recyclerview" android:layout_width="match_parent" android:layout_height=" match_parent"/>
  • 因此,您在 ViewPager 页面中有一个垂直 RecyclerView,并且您希望在 RecyclerView 中垂直向上滚动后隐藏工具栏,对吧?

标签: android android-fragments android-viewpager android-recyclerview material-design


【解决方案1】:

您需要创建一个接口对象,该对象可以在回收器视图中的片段和主要活动之间进行通信。

观看此视频,了解如何在稍有不同的用例中创建此类界面,但我认为它可能会有所帮助,因为它帮助我解决了与您的问题略有相似的问题:

https://www.youtube.com/watch?v=MHHXxWbSaho

干杯!

【讨论】:

    猜你喜欢
    • 2018-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-19
    • 2015-08-12
    相关资源
    最近更新 更多