我需要知道材料设计库中的 CollapsingToolbar 何时折叠。
问问题
40608 次
5 回答
99
正如 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
}
}
}););
于 2016-09-10T08:47:22.993 回答
81
更新:由于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
}
}
于 2015-08-07T08:21:29.090 回答
19
看看这个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
}
}
于 2015-08-07T08:13:01.423 回答
2
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
于 2019-03-18T02:42:17.747 回答
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
}
}
});
于 2018-10-19T11:38:03.773 回答