如何确定CollapsingToolbar是否已折叠?

时间:2015-08-07 08:07:30

标签: android android-design-library android-collapsingtoolbarlayout

我需要知道材料设计库中的CollapsingToolbar何时折叠。

5 个答案:

答案 0 :(得分:67)

更新:由于 23.1.1 + 的支持版本不再存在问题,因此无需使用侦听器并禁用滑动刷新布局,它将起作用应该(link)。

AppBarLayout

上实施 AppBarLayout.OnOffsetChangedListener 监听器
newTexture.wrapS = newTexture.wrapT = THREE.RepeatWrapping;
newTexture.repeat.set(2,2); 

检查offset是否为0,表示工具栏已完全展开。

AppBarLayout appBarLayout = (AppBarLayout) view.findViewById(R.id.app_bar_layout);
appBarLayout.addOnOffsetChangedListener(this);

答案 1 :(得分:65)

正如Marko所说,这可以使用你自己的OnOffsetChangedListener实现来实现。

AppBarLayout appBarLayout = (AppBarLayout) view.findViewById(R.id.app_bar_layout);
appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
        @Override
        public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
            if (Math.abs(verticalOffset) == appBarLayout.getTotalScrollRange()) {
                // Collapsed
            } else if (verticalOffset == 0) {
                // Expanded
            } else {
                // Somewhere in between
            }
        }
    }););

答案 2 :(得分:18)

查看此github gist

public class MyAppBarLayout extends AppBarLayout
        implements AppBarLayout.OnOffsetChangedListener {

    private State state;
    private OnStateChangeListener onStateChangeListener;

    public MyAppBarLayout(Context context) {
        super(context);
    }

    public MyAppBarLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        if (!(getLayoutParams() instanceof CoordinatorLayout.LayoutParams)
                || !(getParent() instanceof CoordinatorLayout)) {
            throw new IllegalStateException(
                    "MyAppBarLayout must be a direct child of CoordinatorLayout.");
        }
        addOnOffsetChangedListener(this);
    }

    @Override
    public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
        if (verticalOffset == 0) {
            if (onStateChangeListener != null && state != State.EXPANDED) {
                onStateChangeListener.onStateChange(State.EXPANDED);
            }
            state = State.EXPANDED;
        } else if (Math.abs(verticalOffset) >= appBarLayout.getTotalScrollRange()) {
            if (onStateChangeListener != null && state != State.COLLAPSED) {
                onStateChangeListener.onStateChange(State.COLLAPSED);
            }
            state = State.COLLAPSED;
        } else {
            if (onStateChangeListener != null && state != State.IDLE) {
                onStateChangeListener.onStateChange(State.IDLE);
            }
            state = State.IDLE;
        }
    }

    public void setOnStateChangeListener(OnStateChangeListener listener) {
        this.onStateChangeListener = listener;
    }

    public interface OnStateChangeListener {
        void onStateChange(State toolbarChange);
    }

    public enum State {
        COLLAPSED,
        EXPANDED,
        IDLE
    }
}

答案 3 :(得分:0)

appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() 
{
        @Override
        public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset)
 {

            if (Math.abs(verticalOffset)-appBarLayout.getTotalScrollRange() == 0)
            {
                //  on Collapse
            }
            else
            {
               //  on expand
            }
        }
    });

答案 4 :(得分:0)

 appBarLayout.addOnOffsetChangedListener(AppBarLayout.OnOffsetChangedListener { appBarLayout, verticalOffset ->

        if (Math.abs(verticalOffset)  >= appBarLayout.totalScrollRange) { // collapse
        } else if (verticalOffset == 0) { // fully expand

        } else { // scolling

        }
    })

appBarLayout.totalScrollRange在某些情况下并不总是等于verticalOffset,最好检查verticalOffset比totoalScrollRange大