如何在android中折叠和更改视图? (不是工具栏)

时间:2017-12-13 07:12:25

标签: android android-toolbar android-collapsingtoolbarlayout

我想在折叠时用另一个视图更改我的视图。我已经使用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>

扩展视图:

Expanded view

这是滚动时我想要的:

This is what I want when scrolled

当使用工具栏更改视图或添加为子视图时,会发生这种情况:

This is what happened when view changed with toolbar or added as child

1 个答案:

答案 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. 

});