3

我将以下代码用于AppBarLayout.

public void showToolbar(boolean show) {
    if (appBar == null) {
        Log.e(TAG, "showToolbar: Toolbar is null");
        return;
    }
    boolean toolbarShown = Utils.isViewVisible(appBar);
    Log.d(TAG, "showToolbar: shown:" +shown);
    boolean changed = (show != toolbarShown);
    if (changed) {
        if (show) {
            Log.d(TAG, "showToolbar: showing");
            appBar.setVisibility(View.VISIBLE);
            appBar.animate()
                    .translationY(0)
                    .setInterpolator(new DecelerateInterpolator())
                    .setListener(new Animator.AnimatorListener() {
                        @Override
                        public void onAnimationStart(Animator animator) {
                            appBar.setVisibility(View.VISIBLE);
                        }

                        @Override
                        public void onAnimationEnd(Animator animator) { }

                        @Override
                        public void onAnimationCancel(Animator animator) { }

                        @Override
                        public void onAnimationRepeat(Animator animator) { }
                    })
                    .start();
        } else {
            Log.d(TAG, "showToolbar: hiding");
            appBar.animate()
                    .translationY(-toolbar.getBottom())
                    .setInterpolator(new DecelerateInterpolator())
                    .setListener(new Animator.AnimatorListener() {
                        @Override
                        public void onAnimationStart(Animator animator) { }

                        @Override
                        public void onAnimationEnd(Animator animator) {
                            appBar.setVisibility(View.INVISIBLE);
                        }

                        @Override
                        public void onAnimationCancel(Animator animator) { }

                        @Override
                        public void onAnimationRepeat(Animator animator) { }
                    })
                    .start();
        }
    } else {
        Log.d(TAG, "showToolbar: no change");
    }
}

除了第一次showToolbar(true)调用显示工具栏外,动画效果很好。该视图第一次显示时没有动画。我已经搜索了该站点并发现了类似的问题,但这些解决方案似乎对我不起作用。

可能值得注意的是,这仅在我们希望appBar首先隐藏时才会发生。我的猜测是,也许动画

更新1:

public static boolean isViewVisible(View view) {
    if (View.VISIBLE == view.getVisibility()) return true;
    else return false;
}

更新 2

我删除了isViewWithinScreenBounds()方法,因为实际上并不需要该检查。

4

2 回答 2

1

试试这个

<android.support.design.widget.AppBarLayout
    android:id="@+id/app_bar"
    android:layout_width="match_parent"
    android:translationY="-120dp"
    android:layout_height="120dp">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="SOME TEXT HERE" />
    </android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>

活动代码

@BindView(R.id.app_bar)
AppBarLayout appBar;
@BindView(R.id.toolbar)
Toolbar toolbar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.some_layout);
    ButterKnife.bind(this);

    setSupportActionBar(toolbar);

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            animateToolbar(false); //Just for testing purpose - delay execute of function animateToolbar() for 4 seconds
        }
    }, 4000);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    animateToolbar(true);
    return super.onCreateOptionsMenu(menu);
}

private void animateToolbar(boolean show) {

    if (show) {
        // As you can see in the xml layout initial position of the appBar is translated by its height to the top of the screen "-" sign
        // Slide int from out of the screen to initial position -> from -120 dp (height of the app bar, see xml) to 0 dp 
        appBar.animate()
                .translationY(0)
                .setDuration(1000)
                .start(); 

    } else {
        // Slide out from initial position to the top of the screen -> from 0 dp to -120 dp (height of the app bar, see xml)
        appBar.animate()
                .translationY(-appBar.getHeight())
                .setDuration(1000)
                .start();
    }
}
于 2016-09-25T13:05:07.110 回答
1

请务必为visibility和设置初始值translationY

如果您希望您的工具栏最初被隐藏并与第一个动画一起显示,请务必设置android:visibility="invisible"and NOT "gone"和否定android:translationYlike -56dp

于 2016-09-25T19:19:33.897 回答