CoordinatorLayout + Fragment + NavigationDrawer

时间:2016-05-23 22:41:17

标签: android android-fragments android-coordinatorlayout android-navigation-drawer android-nestedscrollview

我正在尝试将CoordinatorLayout实施到我的应用中。 我尝试了很多不同的教程,我也尝试在CoordinatorLayout + AppBarLayout + NavigationDrawer上针对我的问题调整解决方案,但我无法弄清楚它为什么不起作用。

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.design.widget.AppBarLayout
    android:id="@+id/id_appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/transparent"
    android:fitsSystemWindows="true">

    <android.support.design.widget.CollapsingToolbarLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        app:contentScrim="?attr/colorPrimary"
        app:expandedTitleMarginEnd="64dp"
        app:expandedTitleMarginStart="48dp"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

        <ImageView
            android:id="@+id/expandedImage"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:scaleType="centerCrop"
            android:src="@drawable/header"
            app:layout_collapseMode="parallax"
            app:layout_collapseParallaxMultiplier="0.7" />

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar_actionbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@android:color/transparent"
            android:minHeight="@dimen/abc_action_bar_default_height_material"
            app:layout_scrollFlags="scroll|enterAlways" />
    </android.support.design.widget.CollapsingToolbarLayout>

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

<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clickable="true">

        <Button
            android:id="@+id/emiter"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_gravity="center_horizontal|top"
            android:layout_marginTop="-70dp"
            android:text="" />
    </FrameLayout>

    <fragment
        android:id="@+id/fragment_drawer"
        android:name="com.myapp.test.navigationdrawer.NavigationDrawerFragment"
        android:layout_width="@dimen/navigation_drawer_width"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:layout="@layout/fragment_navigation_drawer" />
</android.support.v4.widget.DrawerLayout>

协调员本身似乎正在工作,因为我可以得到效果,但只有当&#34;滚动&#34;带有ToolBar的屏幕上半部分。它对于我在ToolBar下面的片段中的ScrollView不起作用。 我做错了什么?

1 个答案:

答案 0 :(得分:1)

  

它不适用于我的片段中的ScrollView

CoordinatorLayout只能监听嵌套滚动。以root身份使用NestedScrollViewRecyclerView查看要添加到Activity中的每个片段,并且CoordinatorLayout将接收滚动事件。

像这样:

您的 activity.xml

let DelayPoint:CGFloat = 10

class DelaySlider: UISlider {

    var startPoint:CGPoint?

    override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {

        if self.startPoint == nil {
          self.startPoint = touches.first?.previousLocationInView(self)
        }


        if self.shouldAllowForSendActionForPoint((touches.first?.locationInView(self))!) {
            super.touchesMoved(touches, withEvent: event)
        }
    }

    override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
        self.startPoint = nil
        super.touchesEnded(touches, withEvent: event)
    }

    func shouldAllowForSendActionForPoint(location:CGPoint) -> Bool {

        if self.startPoint != nil {

            let xDiff = (self.startPoint?.x)! - location.x
            let yDiff = (self.startPoint?.y)! - location.y

            if (xDiff > DelayPoint || xDiff < -DelayPoint || yDiff > DelayPoint || yDiff < -DelayPoint) {

                return true
            }
        }
        return false
    }
}

您的 fragment.xml

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.design.widget.AppBarLayout
    android:id="@+id/id_appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/transparent"
    android:fitsSystemWindows="true">

    <android.support.design.widget.CollapsingToolbarLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        app:contentScrim="?attr/colorPrimary"
        app:expandedTitleMarginEnd="64dp"
        app:expandedTitleMarginStart="48dp"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

        <ImageView
            android:id="@+id/expandedImage"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:scaleType="centerCrop"
            android:src="@drawable/test"
            app:layout_collapseMode="parallax"
            app:layout_collapseParallaxMultiplier="0.7" />

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar_actionbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@android:color/transparent"
            android:minHeight="@dimen/abc_action_bar_default_height_material"
            app:layout_scrollFlags="scroll|enterAlways" />
    </android.support.design.widget.CollapsingToolbarLayout>

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

<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clickable="true">

        <Button
            android:id="@+id/emiter"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_gravity="center_horizontal|top"
            android:layout_marginTop="-70dp"
            android:text="" />
    </FrameLayout>

    <fragment
        android:id="@+id/fragment_drawer"
        class="ru.solodovnikov.test.MainFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</android.support.v4.widget.DrawerLayout>