AppbarLayout appbar_scrolling_view_behavior 导致的ContentFooter显示不全问题

首先看一下问题,使用MD后,原有的下面的布局都是按照整个屏幕尺寸绘制的,被toolbar挤到了下面,如果下面的布局不是nestscroll或者recyclerview他就无法移动。

效果如下:


界面布局如下:

<?xml version="1.0" encoding="utf-8"?>
<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/app_bar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
        <include layout="@layout/toolbar_mainactivity" />
    </android.support.design.widget.AppBarLayout>
    <FrameLayout
        android:id="@+id/id_fragmentMain"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_behavior="com.nuctech.tr.trapp.behavior.FixScrollingFooterBehavior"
       ></FrameLayout>
    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/dp_10"
        android:src="@android:drawable/ic_dialog_email"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>

解决办法:

1.我们编写一个自定义的behavior,重新计算bottomPadding。

2.设置我们自己的behavior。

贴代码:

package com.nuctech.tr.trapp.behavior;

import android.content.Context;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CoordinatorLayout;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by Administrator on 2016/7/1.
 */
public class FixScrollingFooterBehavior extends AppBarLayout.ScrollingViewBehavior {

    private AppBarLayout appBarLayout;

    public FixScrollingFooterBehavior() {
        super();
    }

    public FixScrollingFooterBehavior(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) {

        if (appBarLayout == null) {
            appBarLayout = (AppBarLayout) dependency;
        }

        final boolean result = super.onDependentViewChanged(parent, child, dependency);
        final int bottomPadding = calculateBottomPadding(appBarLayout);
        final boolean paddingChanged = bottomPadding != child.getPaddingBottom();
        if (paddingChanged) {
            child.setPadding(
                    child.getPaddingLeft(),
                    child.getPaddingTop(),
                    child.getPaddingRight(),
                    bottomPadding);
            child.requestLayout();
        }
        return paddingChanged || result;
    }


    // Calculate the padding needed to keep the bottom of the view pager's content at the same location on the screen.
    private int calculateBottomPadding(AppBarLayout dependency) {
        final int totalScrollRange = dependency.getTotalScrollRange();
        return totalScrollRange + dependency.getTop();
    }
}
贴最后引用的代码:

<?xml version="1.0" encoding="utf-8"?>
<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/app_bar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
        <include layout="@layout/toolbar_mainactivity" />
    </android.support.design.widget.AppBarLayout>
    <FrameLayout
        android:id="@+id/id_fragmentMain"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_behavior="com.nuctech.tr.trapp.behavior.FixScrollingFooterBehavior.java"
       ></FrameLayout>
    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/dp_10"
        android:src="@android:drawable/ic_dialog_email"
        app:layout_behavior="com.nuctech.tr.trapp.behavior.ScrollingFabBehavior" />

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

大功告成!


可以使用以下两种方法解决这个问题: 1. 在布局中使用RelativeLayout,将Button放在RecyclerView之上,确保Button的z-index比RecyclerView高。 示例代码: ``` <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <androidx.recyclerview.widget.RecyclerView android:id="@+id/recycler_view" android:layout_width="match_parent" android:layout_height="match_parent"/> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:text="Button"/> </RelativeLayout> ``` 2. 使用CoordinatorLayout,将RecyclerView和Button放在不同的子视图中,并使用app:layout_anchor属性将Button锚定在RecyclerView下方。 示例代码: ``` <androidx.coordinatorlayout.widget.CoordinatorLayout android:layout_width="match_parent" android:layout_height="match_parent"> <androidx.recyclerview.widget.RecyclerView android:id="@+id/recycler_view" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior"/> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|center_horizontal" android:text="Button" app:layout_anchor="@id/recycler_view" app:layout_anchorGravity="bottom"/> </androidx.coordinatorlayout.widget.CoordinatorLayout> ``` 这两种方法都可以让Button在RecyclerView之上,并且不会被RecyclerView覆盖。选择哪种方法取决于你的具体需求和布局结构。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值