Android CollapsingToolbarLayout折叠监听器


106

CollapsingToolBarLayoutAppBarLayout和一起使用CoordinatorLayout,它们一起工作正常。Toolbar向上滚动时,我将其设置为固定,我想知道在CollapsingToolBarLayout折叠时是否有办法更改工具栏的标题文本。

总结一下,在滚动展开时我想要两个不同的标题

谢谢大家

Answers:


150

我分享了基于@Frodio Beggins和@Nifhel代码的完整实现:

public abstract class AppBarStateChangeListener implements AppBarLayout.OnOffsetChangedListener {

    public enum State {
        EXPANDED,
        COLLAPSED,
        IDLE
    }

    private State mCurrentState = State.IDLE;

    @Override
    public final void onOffsetChanged(AppBarLayout appBarLayout, int i) {
        if (i == 0) {
            if (mCurrentState != State.EXPANDED) {
                onStateChanged(appBarLayout, State.EXPANDED);
            }
            mCurrentState = State.EXPANDED;
        } else if (Math.abs(i) >= appBarLayout.getTotalScrollRange()) {
            if (mCurrentState != State.COLLAPSED) {
                onStateChanged(appBarLayout, State.COLLAPSED);
            }
            mCurrentState = State.COLLAPSED;
        } else {
            if (mCurrentState != State.IDLE) {
                onStateChanged(appBarLayout, State.IDLE);
            }
            mCurrentState = State.IDLE;
        }
    }

    public abstract void onStateChanged(AppBarLayout appBarLayout, State state);
}

然后您可以使用它:

appBarLayout.addOnOffsetChangedListener(new AppBarStateChangeListener() {
    @Override
    public void onStateChanged(AppBarLayout appBarLayout, State state) {
        Log.d("STATE", state.name());
    }
});

21
没错 但是请不要使用Proguard将枚举转换为整数值。
rciovati

1
我不知道 那很棒!
tim687

2
枚举也是确保类型安全的一种非常好的方法。您不能拥有State.IMPLODED,因为它不存在(编译器会抱怨),但是使用Integer常量,您可以使用一个值,表示编译器不知道是错误的。它们也很适合单身,但这就是另一个故事。
droppin_science

@droppin_science for android enums check IntDef
David Darias

1
@DavidDarias就个人而言,我发现枚举是一种更为干净的方法,即使它们有开销(此处的自变量... :-)
droppin_science

95

该解决方案非常适合我检测AppBarLayout折叠或展开。

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

            if (Math.abs(verticalOffset)-appBarLayout.getTotalScrollRange() == 0)
            {
                //  Collapsed


            }
            else
            {
                //Expanded


            }
        }
    });

使用addOnOffsetChangedListenerAppBarLayout


36

OnOffsetChangedListener上你的AppBarLayout。当verticalOffset到达的Toolbar高度小于或等于0时,表示CollapsingToolbarLayout已折叠,否则将展开或扩展。

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);
                }

            }
        });

1
它对我不起作用。OnCollapse我想启用主页按钮,并在“展开”上隐藏主页按钮
Maheshwar Ligade

9
当工具栏完全展开时,verticalOffset值似乎为零,然后在折叠时变为负数。当工具栏折叠时,verticalOffset等于工具栏高度的负数(-mToolbar.getHeight())。因此...工具栏已部分展开“ if(verticalOffset> -mToolbar.getHeight())”
Mike

如果有人想知道该appBarLayout.getVerticalOffset()方法在哪里,则可以调用appBarLayout.getY()来检索回调中使用的相同值。
加里特·米拉德

不幸的是Jarett Millard是不对的。取决于您的fitsSystemWindow配置和StatusBar配置(透明)appBarLayout.getY(),可能是verticalOffset = appBarLayout.getY() + statusBarHeight
Capricorn

1
有没有人注意到即使我们实际上并未与Appbar交互,是否也会重复调用mAppBarLayout.addOnOffsetChangedListener(listener)?或者这是我观察此行为的布局/应用程序中的错误。请帮忙!
拉胡尔·舒克拉

16

这段代码对我有用

mAppBarLayout.addOnOffsetChangedListener(new   AppBarLayout.OnOffsetChangedListener() {
        @Override
        public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
            if (verticalOffset == -mCollapsingToolbarLayout.getHeight() + mToolbar.getHeight()) {
                //toolbar is collapsed here
                //write your code here
            }
        }
    });

比尼古拉·德斯波托斯基
Vignesh Bala)

似乎不是一个可靠的解决方案。我对其进行了测试,设备上的值如下:mCollapsingToolbarLayout.getHeight()= 1013,mToolbar.getHeight()=224。因此,根据您的解决方案,处于折叠状态的verticalOffset必须为-789,但等于-693
Leo Droidcoder

16
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;
            }
        }
    });
}

10

您可以使用以下方法获取collapsingToolBar alpha百分比:

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

供参考:链接


2
这是一个很好的答案,因为它提供了标准化的偏移量。我认为,API应该直接提供此值,而不是verticalOffset像素距离。
dbm

5

这是Kotlin解决方案。一个添加OnOffsetChangedListenerAppBarLayout

方法A:

添加AppBarStateChangeListener.kt到您的项目:

import com.google.android.material.appbar.AppBarLayout
import kotlin.math.abs

abstract class AppBarStateChangeListener : AppBarLayout.OnOffsetChangedListener {

    enum class State {
        EXPANDED, COLLAPSED, IDLE
    }

    private var mCurrentState = State.IDLE

    override fun onOffsetChanged(appBarLayout: AppBarLayout, i: Int) {
        if (i == 0 && mCurrentState != State.EXPANDED) {
            onStateChanged(appBarLayout, State.EXPANDED)
            mCurrentState = State.EXPANDED
        }
        else if (abs(i) >= appBarLayout.totalScrollRange && mCurrentState != State.COLLAPSED) {
            onStateChanged(appBarLayout, State.COLLAPSED)
            mCurrentState = State.COLLAPSED
        }
        else if (mCurrentState != State.IDLE) {
            onStateChanged(appBarLayout, State.IDLE)
            mCurrentState = State.IDLE
        }
    }

    abstract fun onStateChanged(
        appBarLayout: AppBarLayout?,
        state: State?
    )

}

将侦听器添加到您的appBarLayout

appBarLayout.addOnOffsetChangedListener(object: AppBarStateChangeListener() {
        override fun onStateChanged(appBarLayout: AppBarLayout?, state: State?) {
            Log.d("State", state.name)
            when(state) {
                State.COLLAPSED -> { /* Do something */ }
                State.EXPANDED -> { /* Do something */ }
                State.IDLE -> { /* Do something */ }
            }
        }
    }
)

方法B:

appBarLayout.addOnOffsetChangedListener(AppBarLayout.OnOffsetChangedListener { appBarLayout, verticalOffset ->
        if (abs(verticalOffset) - appBarLayout.totalScrollRange == 0) { 
            // Collapsed
        } else if (verticalOffset == 0) {
            // Expanded
        } else {
            // Idle
        }
    }
)

3

此解决方案为我工作:

@Override
public void onOffsetChanged(AppBarLayout appBarLayout, int i) {
  if (i == 0) {
    if (onStateChangeListener != null && state != State.EXPANDED) {
      onStateChangeListener.onStateChange(State.EXPANDED);
    }
    state = State.EXPANDED;
  } else if (Math.abs(i) >= 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;
  }
}

在AppBarLayout上使用addOnOffsetChangedListener。


您可以分享完整的代码吗?什么是State.EXPANDED等?
Chetna

1

如果您使用的是CollapsingToolBarLayout,则可以将其

collapsingToolbar.setExpandedTitleColor(ContextCompat.getColor(activity, android.R.color.transparent));
collapsingToolbar.setTitle(title);

1

该代码对我来说是完美的。您可以使用百分比刻度

@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时得到-582,因此我可以通过在Toast中设置offsetvalue来找到值,并相应地更改代码。

 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");
            }
        }
    });
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.