协调器布局中的底部视图在向下滚动之前不可见

时间:2017-01-29 09:05:35

标签: android android-recyclerview android-coordinatorlayout

我正在尝试在启动动作模式时在Recyclerview中显示Coordinatorlayout下面的视图。但直到我向下滚动底部视图是不可见的。如果我删除了app:layout_behavior="@string/appbar_scrolling_view_behavior", 我得到了我想要的东西,然后Recyclerview在工具栏下面。有关如何使用Coordinatorlayout始终显示视图的任何建议。

主要布局:

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


<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <include layout="@layout/toolbar" />
</android.support.design.widget.AppBarLayout>


    <include layout="@layout/list" />
</android.support.design.widget.CoordinatorLayout>

列表布局:

<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:orientation="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior">


<LinearLayout                               
    android:id="@+id/bottomView"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:layout_alignParentBottom="true"
    android:background="@color/colorPrimary"
    android:orientation="vertical">

</LinearLayout>

<android.support.v4.widget.SwipeRefreshLayout
    android:id="@+id/swipeRefreshLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/layoutDummy">


    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

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

</RelativeLayout>

2 个答案:

答案 0 :(得分:0)

使用&#34; layout_weight&#34;尝试以下更改它应该计算视图高度而不会将您的下方视图推到可见边框之外。 (它在运行时计算可用的高度)。

(我假设您要在Recycler视图下方显示的视图是linerlayout)

&#13;
&#13;
<LinearLayout 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:orientation="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior">




<android.support.v4.widget.SwipeRefreshLayout
    android:id="@+id/swipeRefreshLayout"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"                                          
    android:layout_above="@+id/layoutDummy">


    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

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

  <LinearLayout                               
    android:id="@+id/bottomView"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:background="@color/colorPrimary"
    android:orientation="vertical">

</LinearLayout>
  
</LinearLayout>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

通过直接将底视图作为子项添加到协调器布局并使其在启动操作模式下可见,我得到了一个解决方案。但是这个解决方案的缺点是底视图隐藏了Recyclerview的最后一行。所以我必须在Recyclerview的末尾添加一个空页脚。更好的解决方案是最受欢迎的。

主要布局:

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


    <android.support.design.widget.AppBarLayout
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:theme="@style/AppTheme.AppBarOverlay">

        <include layout="@layout/toolbar" />
    </android.support.design.widget.AppBarLayout>


        <include layout="@layout/list" />

 <!-- BOTTOM VIEW -->
    <LinearLayout                               
       android:id="@+id/bottomView"   
       android:layout_width="match_parent"
       android:layout_height="200dp"
       android:layout_alignParentBottom="true"
       android:background="@color/colorPrimary"
       android:orientation="vertical"/>
    </android.support.design.widget.CoordinatorLayout>

列表布局:

 <LinearLayout 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:orientation="vertical"
         app:layout_behavior="@string/appbar_scrolling_view_behavior">


    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipeRefreshLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/layoutDummy">


    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

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

    </LinearLayout>

适配器:

private static final int TYPE_ITEM = 1;
private static final int TYPE_FOOTER = 2;

 @Override
public int getItemViewType(int position) {
    if (isPositionFooter(position)) {
        return TYPE_FOOTER;
    }
    return TYPE_ITEM;
}

private boolean isPositionFooter(int position) {
    return position == list.size();
}


@Override
public int getItemCount() {
    if (list == null) {
        return 0;
    } else {
        return list.size() + 1;
    }
}