Android - 在 CoordinatorLayout 中使用时页脚会滚动到屏幕外 [英] Android - footer scrolls off screen when used in CoordinatorLayout

查看:26
本文介绍了Android - 在 CoordinatorLayout 中使用时页脚会滚动到屏幕外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 AppBarLayout,它在滚动 RecyclerView 时会滚出屏幕.在 RecyclerView 下方有一个 RelativeLayout,它是一个页脚.

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

layout_scrollFlags="scroll|enterAlways"

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

滚动前

滚动后

更新

就此打开了一个谷歌问题 - 它被标记'WorkingAsIntended' 这仍然没有帮助,因为我想要一个片段内页脚的工作解决方案.

更新 2

您可以在此处找到活动和片段

请注意,如果 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.

布局:

<android.support.design.widget.AppBarLayoutandroid:id="@+id/appbar"android:layout_width="match_parent"android:layout_height="wrap_content"><android.support.v7.widget.Toolbarandroid:id="@+id/工具栏"android:layout_width="match_parent"android:layout_height="?android:attr/actionBarSize"android:minHeight="?android:attr/actionBarSize"应用程序:标题=Foo"app:layout_scrollFlags="scroll|enterAlways|snap"/><android.support.design.widget.TabLayoutandroid:id="@+id/标签"android:layout_width="match_parent"android:layout_height="wrap_content"app:tabMode="fixed"/></android.support.design.widget.AppBarLayout><android.support.v4.view.ViewPagerandroid: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 {私有 AppBarLayout appBarLayout;公共 FixScrollingFooterBehavior() {极好的();}公共 FixScrollingFooterBehavior(上下文上下文,AttributeSet attrs){超级(上下文,属性);}@覆盖public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) {如果(appBarLayout == null){appBarLayout = (AppBarLayout) 依赖项;}最终布尔结果 = super.onDependentViewChanged(parent, child, dependency);final int bottomPadding = calculateBottomPadding(appBarLayout);final boolean paddingChanged = bottomPadding != child.getPaddingBottom();如果(填充更改){child.setPadding(child.getPaddingLeft(),child.getPaddingTop(),child.getPaddingRight(),底部填充);child.requestLayout();}返回 paddingChanged ||结果;}//计算将视图分页器内容的底部保持在屏幕上相同位置所需的填充.私有 int calculateBottomPadding(AppBarLayout 依赖项){final int totalScrollRange = dependency.getTotalScrollRange();返回 totalScrollRange + dependency.getTop();}}

I have an AppBarLayout that scrolls off screen when scrolling a RecyclerView. Below the RecyclerView there is a RelativeLayout that is a footer.

The footer is shown only after scrolling up - it behave like it has

layout_scrollFlags="scroll|enterAlways"

but it doesn't have any scroll flags - is it a bug or am I doing something wrong? I want it to be always visible

before scroll

after scroll

Update

opened a google issue on this - it was marked 'WorkingAsIntended' this still doesn't help because I want a working solution of a footer inside a fragment.

Update 2

you can find the activity and the fragment xmls here -

note that if line 34 in activity.xml - the line containing app:layout_behavior="@string/appbar_scrolling_view_behavior" is commented out the text end is visible from the start - otherwise, it is visible only after scrolling up

解决方案

I use a simplified version of Learn OpenGL ES's solution (https://stackoverflow.com/a/33396965/778951) -- which improves on Noa's solution (https://stackoverflow.com/a/31140112/1317564). It works fine for my simple quick-return toolbar above a TabLayout with footer buttons in each tab's ViewPager content.

Just set the FixScrollingFooterBehavior as the layout_behavior on the View/ViewGroup you want to keep aligned at the bottom of the screen.

Layout:

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

Behavior:

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 中使用时页脚会滚动到屏幕外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆