【问题标题】:implementing a custom behavior on recyclerview inside coordinatorlayout在 coordinatorlayout 内的 recyclerview 上实现自定义行为
【发布时间】:2015-12-17 09:44:06
【问题描述】:

我的布局文件中有 3 个视图,其中 CoordinatorLayout 作为根视图:AppbarLayout、RecyclerView 和页脚(始终不可见)。 Recyclerview 实现了默认行为 appbar_scrolling_view_behavior,理想情况下,将 recyclerview 置于 appbarlayout 之下。但是recyclerview和页脚重叠。为了防止这种情况,我必须编写一个自定义行为,以便当页脚可见时,Recyclerview 应该为页脚腾出空间。但是现在,appbar_scrolling_view_behavior 的默认行为消失了,现在 appbarlayout 和 recyclerview 重叠了。Here is the Image of the layout

链接到我实现的自定义行为: https://github.com/Mandeep221/CustomBehaviorForRecyclerview/blob/master/CustomBehavior.java

我的问题:我怎样才能实现两件事(同时在一个行为中):

  1. 在 co-ordinatorlayout 内的 appbarlayout 下方获取 recyclerview
  2. 如果页脚可见,请获取 recyclerview 为页脚腾出空间。

如果您能提出一些解决方法,那就太好了!非常感谢!

<android.support.design.widget.AppBarLayout
    android:id="@+id/appBarLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.Toolbar
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways|snap" />

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

<android.support.v7.widget.RecyclerView 
xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/list_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="4dp"
        android:scrollbars="vertical"
      app:layout_behavior="prokure.it.prokure.Search.NewSearch.CustomBehavior" />


    <prokure.it.prokure.FooterBarLayout
        android:id="@+id/footerBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

        //footer child views    

            </LinearLayout>
    </prokure.it.prokure.FooterBarLayout>

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

【问题讨论】:

    标签: android android-recyclerview android-coordinatorlayout android-appbarlayout


    【解决方案1】:

    不要将 android.support.design.widget.CoordinatorLayout 作为根布局。

    添加一个 RelativeLayout 作为根布局。然后添加带有属性 layout_alignParentTop 和 layout_above="@+id/footerBar" 的 android.support.design.widget.CoordinatorLayout 并将 prokure.it.prokure.FooterBarLayout 设置为 align parent bottom 作为 RelativeLayout 的第二个孩子。

    <RelativeLayout
        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.CoordinatorLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_above="@+id/footerBar">
    
            <android.support.design.widget.AppBarLayout
                android:id="@+id/appBarLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
    
                <android.support.v7.widget.Toolbar
                    android:id="@+id/app_bar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    android:background="?attr/colorPrimary"
                    app:layout_scrollFlags="scroll|enterAlways|snap" />
    
            </android.support.design.widget.AppBarLayout>
    
            <android.support.v7.widget.RecyclerView 
                android:id="@+id/list_recycler_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginTop="4dp"
                android:scrollbars="vertical"
                 />
        </android.support.design.widget.CoordinatorLayout>
    
        <prokure.it.prokure.FooterBarLayout
            android:id="@+id/footerBar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom">
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">
    
                //footer child views    
    
            </LinearLayout>
        </prokure.it.prokure.FooterBarLayout>
    </RelativeLayout>
    

    【讨论】:

      【解决方案2】:

      另一种更简单的方法是将以下行添加到您的 prokure.it.prokure.FooterBarLayout

          app:layout_anchor="@id/list_recycler_view"
          app:layout_anchorGravity="bottom"
      

      并将 prokure.it.prokure.FooterBarLayout 保留在协调器布局中。

      确保在回收器视图的末尾添加一个空单元格,以便 prokure.it.prokure.FooterBarLayout 不会与 RecyclerView 的最后一个单元格重叠。

      【讨论】:

        猜你喜欢
        • 2015-11-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-03-22
        • 2017-12-02
        • 1970-01-01
        • 2016-05-17
        • 2018-03-06
        相关资源
        最近更新 更多