如何将元素放置在coordinatorlayout中?

时间:2019-04-18 15:35:22

标签: android android-coordinatorlayout

我想在用户滚动recyclerview时执行“隐藏”导航栏。我发现了如何使用layout_behavior做到这一点,并创建了NavigationBehavior类来隐藏导航栏。但是现在navbar与recyclerview重叠了。如何解决?也许这是隐藏导航栏而不使用coordinatorlayout的另一种方法?请帮忙!!!

xml文件

<android.support.design.widget.CoordinatorLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        tools:context=".fragments.NewsFragment"
        android:orientation="vertical"
>

              <android.support.design.widget.NavigationView
                      xmlns:app="http://schemas.android.com/apk/res-auto"
                      android:background="@color/colorPrimary"
                      android:layout_height="wrap_content"
                      android:layout_width="match_parent"
                      xmlns:android="http://schemas.android.com/apk/res/android"
                      app:elevation="1dp"
                      android:id="@+id/topbar"
                      app:layout_behavior="com.develop.grizzzly.pediatry.views.NavigationBehavior"
              >

                  <RelativeLayout
                          android:layout_height="wrap_content"
                          android:layout_width="match_parent"
                          android:paddingTop="10dp"
                          android:paddingBottom="10dp"
                          android:paddingRight="@dimen/nav_padding"
                          android:paddingLeft="@dimen/nav_padding"
                          android:layout_gravity="bottom"
                  >

                      <ImageView
                              android:background="?selectableItemBackgroundBorderless"
                              android:layout_height="@dimen/nav_icon_width"
                              android:layout_width="@dimen/nav_icon_width"
                              android:id="@+id/menu_button"
                              android:src="@drawable/ic_menu"
                              android:layout_alignParentStart="true"
                              android:layout_centerVertical="true"
                      />

                      <TextView
                              android:id="@+id/nav_title"
                              android:layout_height="wrap_content"
                              android:layout_width="wrap_content"
                              android:textSize="@dimen/title"
                              android:layout_centerInParent="true"
                              android:text="@string/news_title"
                              android:fontFamily="@font/gotham"
                              android:textColor="@color/colorTitle"
                      />

                      <ImageView
                              android:background="?selectableItemBackgroundBorderless"
                              android:layout_height="@dimen/profile_icon_width"
                              android:layout_width="@dimen/profile_icon_width"
                              android:id="@+id/profile_button"
                              android:transitionName="imageTransition"
                              android:layout_alignParentEnd="true"
                              android:src="@mipmap/ic_profile_img"/>

                  </RelativeLayout>

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

                 <android.support.v7.widget.RecyclerView
                         android:id="@+id/news_recycler_view"
                         android:layout_width="match_parent"
                         android:layout_height="match_parent"
                         android:orientation="vertical"
                         android:background="@color/colorBackground"
                 ></android.support.v7.widget.RecyclerView>


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

NavigationBehavior

class NavigationBehavior<V : View>(context: Context, attrs: AttributeSet) :
    CoordinatorLayout.Behavior<V>(context, attrs) {

    override fun onStartNestedScroll(
        coordinatorLayout: CoordinatorLayout, child: V, directTargetChild: View, target: View, axes: Int, type: Int
    ): Boolean {
        return axes == ViewCompat.SCROLL_AXIS_VERTICAL
    }

    override fun onNestedPreScroll(
        coordinatorLayout: CoordinatorLayout, child: V, target: View, dx: Int, dy: Int, consumed: IntArray, type: Int
    ) {
        super.onNestedPreScroll(coordinatorLayout, child, target, dx, dy, consumed, type)
        child.translationY = min(0f, max(-child.height.toFloat(), child.translationY - dy))
    }
}

0 个答案:

没有答案