创建具有最小高度的自定义AppBarLayout子级? [英] Create custom AppBarLayout child with minimum height?

查看:420
本文介绍了创建具有最小高度的自定义AppBarLayout子级?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个自定义视图,该视图将成为AppBarLayout的子级.我需要此视图在向上滚动时部分折叠,但不能完全折叠.它将具有最小高度,并在其小尺寸模式下固定在AppBarLayout的顶部,然后在向下滚动视图时扩展回它的大尺寸模式.

I want to create a custom view that will be a child of an AppBarLayout. I need this view to collapse partially as I scroll up, but not completely. It will have a minimum height and stay fixed to the top of the AppBarLayout in it's small size mode and then expand back to it's large size mode when the view is scrolled back down.

我花了很多时间查看AppBarLayout和CoordinatorLayout的源代码,到目前为止,我还没有找到一种实现自己想要的方法的方法.看起来当向上滚动视图时,AppBarLayout的子级必须保持可见或完全消失.

I've spent a lot of time looking through the source of the AppBarLayout and CoordinatorLayout, and so far I don't see a way to do what I want. It looks like children of AppBarLayout must either stay visible or disappear completely when the view is scrolled up.

有人可以建议一种方法来创建AppBarLayout的子代吗?

Can anyone suggest a way to create a child of an AppBarLayout that will behave this way?

谢谢

推荐答案

这是食谱:

如果设置android:minHeight,则AppBarLayout将通过不滚动超出使组件更小的点来尊重该值.因此,您的XML布局可能是这样的:

If you set android:minHeight, the AppBarLayout will respect that value by not scrolling beyond the point that would make your component smaller. So your XML layout might be something like this:

    <com.example.CustomCollapsingLayout
        android:layout_width="match_parent"
        android:layout_height="320dp"
        android:minHeight="108dp"
        android:fitsSystemWindows="true"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

接下来,您要让您的班级向父级AppBarLayout注册一个OnOffsetChangedListener.滚动应用栏时,组件将获得事件,以便您了解如何配置视图.

Next you want to have your class register an OnOffsetChangedListener with the parent AppBarLayout. Your component will get events as the app bar is scrolled so that you know how to configure your view.

class OnOffsetChangedListener implements AppBarLayout.OnOffsetChangedListener {

        @Override
        public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {

            final int scrollRange = appBarLayout.getTotalScrollRange();
            float offsetFactor = (float) (-verticalOffset) / (float) scrollRange;
            ...

这将向您展示如何找到总滚动范围,然后找到总滚动范围与当前滚动位置(即应用栏在其滚动位置的比例)之间的比率.

This shows you how to find the total scroll range and then find the ratio between the total scroll range and the current scroll position i.e. where the app bar is in its scroll.

您应该执行CollapsingToolbarLayout的操作;覆盖onAttachedToWindow并在此处添加侦听器:

You should do what CollapsingToolbarLayout does; override onAttachedToWindow and add the listener there:

        // Add an OnOffsetChangedListener if possible
        final ViewParent parent = getParent();
        if (parent instanceof AppBarLayout) {
            if (mOnOffsetChangedListener == null) {
                mOnOffsetChangedListener = new OnOffsetChangedListener();
            }
            ((AppBarLayout) parent).addOnOffsetChangedListener(mOnOffsetChangedListener);
        }

看看 CollapsingToolbarLayout 的源代码,因为它会给您一些想法.您的视图需要做很多相同的事情.

Take a look at the source code for CollapsingToolbarLayout as it will give you some ideas. Your view needs to do a lot of the same things.

您还可以查看我的示例项目,该示例项目具有随着滚动工具栏而缩放和移动的图像: https://github.com/klarson2/Collapsing-Image

You can also look at my sample project that has an image that scales and moves as the toolbar is scrolled: https://github.com/klarson2/Collapsing-Image

这篇关于创建具有最小高度的自定义AppBarLayout子级?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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