Android CollapsingToolbarLayout collapse Listener

后端 未结 12 1154
情歌与酒
情歌与酒 2020-12-12 11:37

I am using CollapsingToolBarLayout alongside with AppBarLayout and CoordinatorLayout, and they are working Fine altogether. I set my <

相关标签:
12条回答
  • 2020-12-12 12:17

    This code is working perfect for me. You can use percentage scale How you like

    @Override
    public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
        double percentage = (double) Math.abs(verticalOffset) / collapsingToolbar.getHeight();
        if (percentage > 0.8) {
            collapsingToolbar.setTitle("Collapsed");
        } else {
            collapsingToolbar.setTitle("Expanded");
        }
    }
    
    0 讨论(0)
  • 2020-12-12 12:18

    You can get collapsingToolBar alpha percentage using below :

    appbarLayout.addOnOffsetChangedListener( new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                float percentage = ((float)Math.abs(verticalOffset)/appBarLayout.getTotalScrollRange());
                fadedView.setAlpha(percentage);
        });
    

    For Reference : link

    0 讨论(0)
  • 2020-12-12 12:18

    My Toolbar offset value get -582 when collapse, on expand=0 So I find value by setting offsetvalue in Toast & change code accordingly.

     mAppBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                if(verticalOffset == -582) {
                Toast.makeText(MainActivity.this, "collaped" + verticalOffset, Toast.LENGTH_SHORT).show();
                mCollapsingToolbarLayout.setTitle("Collapsed");
                }else if(verticalOffset == 0){
                    Toast.makeText(MainActivity.this, "expanded" + verticalOffset, Toast.LENGTH_SHORT).show();
                mCollapsingToolbarLayout.setTitle("expanded");
                }
            }
        });
    
    0 讨论(0)
  • 2020-12-12 12:22
    private enum State {
        EXPANDED,
        COLLAPSED,
        IDLE
    }
    
    private void initViews() {
        final String TAG = "AppBarTest";
        final AppBarLayout mAppBarLayout = findViewById(R.id.appbar);
        mAppBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            private State state;
    
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                if (verticalOffset == 0) {
                    if (state != State.EXPANDED) {
                        Log.d(TAG,"Expanded");
                    }
                    state = State.EXPANDED;
                } else if (Math.abs(verticalOffset) >= appBarLayout.getTotalScrollRange()) {
                    if (state != State.COLLAPSED) {
                        Log.d(TAG,"Collapsed");
                    }
                    state = State.COLLAPSED;
                } else {
                    if (state != State.IDLE) {
                        Log.d(TAG,"Idle");
                    }
                    state = State.IDLE;
                }
            }
        });
    }
    
    0 讨论(0)
  • 2020-12-12 12:22

    If you are using CollapsingToolBarLayout you can put this

    collapsingToolbar.setExpandedTitleColor(ContextCompat.getColor(activity, android.R.color.transparent));
    collapsingToolbar.setTitle(title);
    
    0 讨论(0)
  • 2020-12-12 12:32

    Hook a OnOffsetChangedListener to your AppBarLayout. When the verticalOffset reaches 0 or less than the Toolbar height, it means that CollapsingToolbarLayout has collapsed, otherwise it is expanding or expanded.

    mAppBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
                @Override
                public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                    if(verticalOffset == 0 || verticalOffset <= mToolbar.getHeight() && !mToolbar.getTitle().equals(mCollapsedTitle)){
                        mCollapsingToolbar.setTitle(mCollapsedTitle);
                    }else if(!mToolbar.getTitle().equals(mExpandedTitle)){
                        mCollapsingToolbar.setTitle(mExpandedTitle);
                    }
    
                }
            });
    
    0 讨论(0)
提交回复
热议问题