Android 崩溃工具栏布局崩溃监听器

我正在使用 CollapsingToolBarLayoutAppBarLayoutCoordinatorLayout一起,他们是工作良好。我设置我的 Toolbar是固定的,当我向上滚动时,我想知道是否有一种方法来改变工具栏的标题文本,当 CollapsingToolBarLayout是折叠的。

总之,我要两个不同的 头衔滚动和时 扩大

先谢谢大家

78561 次浏览

把一个 OnOffsetChangedListener挂到你的 AppBarLayout上。当 verticalOffset达到0或小于 Toolbar高度时,这意味着 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);
}


}
});

这个解决方案对我很有效:

@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。

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

我分享了完整的实现,基于@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());
}
});

这个代码对我很有用

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

这个解决方案非常适合我检测 AppBarLayout崩溃或扩展。

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


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




}
else
{
//Expanded




}
}
});

AppBarLayout上使用 addOnOffsetChangedListener

如果您正在使用 CollapsingToolBarLayout,您可以将此

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

你可以使用下面的方法得到折叠工具栏的 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);
});

参考资料: 链接

这个代码对我来说是完美的。你可以使用百分比尺度如何你喜欢

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

当折叠时,我的工具栏偏移量值为 -582,在展开时 = 0 所以我通过在 Toast & change 代码中设置偏移值来找到值。

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

这是一个 科特林解决方案。在 AppBarLayout中加入一个 OnOffsetChangedListener

解决方案 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
}
}
)

以下是 Kotlin 的解决方案:

abstract class AppBarStateChangeListener : OnOffsetChangedListener {
enum class State {
EXPANDED, COLLAPSED, IDLE
}


private var mCurrentState =
State.IDLE


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


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

下面是听众:

  appbar.addOnOffsetChangedListener(object : AppBarStateChangeListener() {
override fun onStateChanged(
appBarLayout: AppBarLayout?,
state: State?
) {
if(state == State.COLLAPSED){
LayoutBottom.visibility = View.GONE
}else if(state == State.EXPANDED){
LayoutBottom.visibility = View.VISIBLE
}
}
})