检测 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天全站免登陆