【问题标题】:Bottom view in coordinator layout not visible until scrolled downcoordinatorlayout 中的底部视图在向下滚动之前不可见
【发布时间】:2017-06-14 15:01:31
【问题描述】:

我正在尝试在启动操作模式时在Coordinatorlayout 中的Recyclerview 下方显示一个视图。但直到我向下滚动底部视图是不可见的。如果我删除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>

【问题讨论】:

    标签: android android-recyclerview android-coordinatorlayout


    【解决方案1】:

    尝试下面的更改,通过使用“layout_weight”属性,它应该计算视图高度,而不会将下面的视图推到可见边界之外。 (它计算运行时的可用高度)。

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

    <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>

    【讨论】:

    • 它不起作用。滚动时只有底部视图可见。协调器布局的滚动行为对此负责。
    【解决方案2】:

    我有一个解决方案,方法是直接将底部视图作为子视图添加到协调器布局并使其在启动操作模式时可见。但是这个解决方案的缺点是底视图隐藏了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;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-02
      • 1970-01-01
      • 2015-12-20
      • 2020-06-30
      • 1970-01-01
      • 1970-01-01
      • 2016-01-12
      • 2016-12-27
      相关资源
      最近更新 更多