我想在折叠时用另一个视图更改我的视图。我已经使用CollapsingToolbarLayout
做了几乎相同的事情,但它仅适用于Toolbar
。当我将Toolbar
替换为其他视图或将视图添加为Toolbar
的子视图时,它无法正常工作。
我已经尝试过这段代码了。
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
>
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/aaaa"
android:layout_width="match_parent"
android:layout_height="350dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed|snap"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleTextAppearance="@android:color/transparent"
android:fitsSystemWindows="true"
>
<ImageView
app:layout_collapseMode="parallax"
android:src="@drawable/firework"
android:contentDescription="@string/app_name"
android:scaleType="centerCrop"
android:layout_width="match_parent"
android:layout_height="350dp" />
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="100dp"
android:title="@string/recent_news"
>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.CollapsingToolbarLayout>
答案 0 :(得分:0)
如果您希望在展开AppBarLayout
时更改视图,则可以在案例中按addOnOffsetChangedListener
收听其状态
1-创建您自己的OnOffsetChangedListener
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);
}
2-将OffsetChangedListener
添加到AppBarLayout
appBarLayout.addOnOffsetChangedListener(new AppBarStateChangeListener() {
@Override
public void onStateChanged(AppBarLayout appBarLayout, State state) {
if (state == State.EXPANDED) {
//appbar is expand show the view when it expanded
} else if (state == State.COLLAPSED) {
//appbar is collapsed show the view when it collapsed.
});