如何确定 CollapsingToolbar 已折叠?
How can I determine that CollapsingToolbar is collapsed?
我需要知道 material 设计库中的 CollapsingToolbar 何时折叠。
看看这个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
}
}
UPDATE: 由于支持 23.1.1+ 版本问题不再存在,无需使用侦听器和禁用滑动刷新布局,它将正常工作 ()。
在 AppBarLayout
上实施 AppBarLayout.OnOffsetChangedListener 侦听器
AppBarLayout appBarLayout = (AppBarLayout) view.findViewById(R.id.app_bar_layout);
appBarLayout.addOnOffsetChangedListener(this);
并检查偏移量是否为 0,这意味着工具栏已完全展开。
@Override
public void onOffsetChanged(AppBarLayout appBarLayout, int offset)
{
if (offset == 0)
{
// Fully expanded
}
else
{
// Not fully expanded or collapsed
}
}
正如 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
}
}
}););
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
}
}
});
appBarLayout.addOnOffsetChangedListener(AppBarLayout.OnOffsetChangedListener { appBarLayout, verticalOffset ->
if (Math.abs(verticalOffset) >= appBarLayout.totalScrollRange) { // collapse
} else if (verticalOffset == 0) { // fully expand
} else { // scolling
}
})
appBarLayout.totalScrollRange 在某些情况下并不总是等于 verticalOffset,最好检查大于 totoalScrollRange
的 verticalOffset
我需要知道 material 设计库中的 CollapsingToolbar 何时折叠。
看看这个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
}
}
UPDATE: 由于支持 23.1.1+ 版本问题不再存在,无需使用侦听器和禁用滑动刷新布局,它将正常工作 (
在 AppBarLayout
上实施 AppBarLayout.OnOffsetChangedListener 侦听器AppBarLayout appBarLayout = (AppBarLayout) view.findViewById(R.id.app_bar_layout);
appBarLayout.addOnOffsetChangedListener(this);
并检查偏移量是否为 0,这意味着工具栏已完全展开。
@Override
public void onOffsetChanged(AppBarLayout appBarLayout, int offset)
{
if (offset == 0)
{
// Fully expanded
}
else
{
// Not fully expanded or collapsed
}
}
正如 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
}
}
}););
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
}
}
});
appBarLayout.addOnOffsetChangedListener(AppBarLayout.OnOffsetChangedListener { appBarLayout, verticalOffset ->
if (Math.abs(verticalOffset) >= appBarLayout.totalScrollRange) { // collapse
} else if (verticalOffset == 0) { // fully expand
} else { // scolling
}
})
appBarLayout.totalScrollRange 在某些情况下并不总是等于 verticalOffset,最好检查大于 totoalScrollRange
的 verticalOffset