Android - 在 CoordinatorLayout 中使用时页脚滚动到屏幕外

标签 android android-5.0-lollipop android-design-library android-coordinatorlayout

我有一个 AppBarLayout,它在滚动 RecyclerView 时会滚动到屏幕外。 在 RecyclerView 下面有一个 RelativeLayout 是一个页脚。

页脚仅在向上滚动后显示 - 它的行为就像它有

layout_scrollFlags="scroll|enterAlways"

但它没有任何滚动标志 - 是错误还是我做错了什么?我希望它始终可见

滚动前

enter image description here

滚动后

enter image description here

更新

打开了google issue关于这一点 - 它被标记为“WorkingAsIntended”这仍然没有帮助,因为我想要一个 fragment 内页脚的工作解决方案。

更新 2

you can find the activity and the fragment xmls here -

请注意,如果 activity.xml 中的第 34 行 - 包含 app:layout_behavior="@string/appbar_scrolling_view_behavior" 的行被注释掉了文本 end 从一开始就可见 - 否则,仅在向上滚动后可见

最佳答案

我使用 Learn OpenGL ES 解决方案的简化版本 (https://stackoverflow.com/a/33396965/778951) -- 它改进了 Noa 的解决方案 (https://stackoverflow.com/a/31140112/1317564)。它适用于我在 TabLayout 上方的简单快速返回工具栏,每个选项卡的 ViewPager 内容中都有页脚按钮。

只需将 FixScrollingFooterBehavior 设置为要在屏幕底部保持对齐的 View/ViewGroup 上的 layout_behavior。

布局:

<?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:layout_width="match_parent"
    android:layout_height="match_parent">

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

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?android:attr/actionBarSize"
                android:minHeight="?android:attr/actionBarSize"
                app:title="Foo"
                app:layout_scrollFlags="scroll|enterAlways|snap"
                />

            <android.support.design.widget.TabLayout
                android:id="@+id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:tabMode="fixed"/>

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

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="com.spreeza.shop.ui.widgets.FixScrollingFooterBehavior"
        />

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

行为:

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();
    }
}

关于Android - 在 CoordinatorLayout 中使用时页脚滚动到屏幕外,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30777698/

相关文章:

android - 为什么中介测试套件找不到横幅广告的 Adcolony 广告来源?

使用 Mockito 时不会模拟 Android 方法

android - 方向改变后,针对 "layout-ldltr"加载 "layout-ldrtl"布局中的 android 错误吗?

android - 使用 AppCompat ActionBarActivity 更改状态栏颜色

java - 覆盖android Material 设计警报对话框

安卓后台应用

android - 不向当前 Activity 堆栈添加新 Activity

android - 如何在 Bottom Sheet View 中添加阴影?

android - fab.show() 在初始化新 Activity 后第一次没有动画

android - 我可以使用 Android 官方库使用 Android float 操作按钮吗?