检测AppBarLayout/CollapsingToolbarLayout何时完全展开 [英] Detecting when AppBarLayout/CollapsingToolbarLayout is completely expanded

本文介绍了检测AppBarLayout/CollapsingToolbarLayout何时完全展开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用新CoordinatorLayout/AppBarLayout/CollapsingToolbarLayout范例的片段,我希望能够检测到折叠工具栏何时完全展开,以便我可以对其所在的整个片段执行操作,例如将片段从堆栈中弹出,然后转到一个新片段,将片段解散.我的解雇代码正在运行,我只需要知道何时以及何时不使用它.

I have a fragment that uses the new CoordinatorLayout/AppBarLayout/CollapsingToolbarLayout paradigm, and I'd like to be able to detect when the collapsing toolbar is fully expanded so that I can perform an operation on the entire fragment it's in, e.g. popping the fragment off the stack and going to a new one, dismissing the fragment. I have the dismissing code working, I just need to know when and when not to use it.

我已经尝试了 AppBarLayout.OnOffsetChangedListener ,但是运气不好.有没有一种方法可以用来确定事物何时完全扩展,或者有人知道一种更优选的方法?

I've experimented a bit with AppBarLayout.OnOffsetChangedListener, but didn't have much luck. Is there a way to use it to determine when things are completely expanded, or is there a more preferred method someone knows about?

提前谢谢!

我还看到 AppBarLayout.setExpanded(. ),但不是AppBarLayout.getExpanded()或类似的东西,所以我也很困惑.

I also see there are a couple implementations for AppBarLayout.setExpanded(...), however not AppBarLayout.getExpanded() or something similar, so I'm stumped there too.

推荐答案

看起来API中没有任何内容,但以下内容似乎对我有用.它可能需要测试.

It doesn't look like there's anything in the APIs, but the following seems to be working for me. It might need testing.

boolean fullyExpanded =
    (appBarLayout.getHeight() - appBarLayout.getBottom()) == 0;

上面的解决方案似乎确实有效,但是由于我想在滚动应用栏时测试这种情况,所以最终我将以下解决方案与OnOffsetChangedListener一起使用.

The above solution does seem to work, but since I wanted to test this condition when the appbar was scrolled, I ended up using the following solution with OnOffsetChangedListener.

class Frag extends Fragment implements AppBarLayout.OnOffsetChangedListener {

    private boolean appBarIsExpanded = true;
    private AppBarLayout appBarLayout;

    @Override 
    public void onActivityCreated(Bundle state) {
        super.onActivityCreated(state);
        appBarLayout = (AppBarLayout) getActivity().findViewById(R.id.app_bar);
    }

    @Override
    public void onResume() {
        super.onResume();
        appBarLayout.addOnOffsetChangedListener(this);
    }

    @Override
    public void onStop() {
        super.onStop();
        appBarLayout.removeOnOffsetChangedListener(this);
    }

    @Override
    public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
        appBarIsExpanded = (verticalOffset == 0);
    } 

}

这篇关于检测AppBarLayout/CollapsingToolbarLayout何时完全展开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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