实现的效果如下:
如果需要的只是折叠效果 可以看这遍文章点击跳转!!!!!!!!!!!!!!!!!!!!
具体代码如下
findViewById(R.id.tv).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CoordinatorLayout.Behavior behavior =
((CoordinatorLayout.LayoutParams) appbarlayout
.getLayoutParams())
.getBehavior();
if (behavior instanceof AppBarLayout.Behavior) {
AppBarLayout.Behavior appBarLayoutBehavior = (AppBarLayout
.Behavior) behavior;
int hight = appbarlayout.getHeight();
// 滑动 计算的高度
appBarLayoutBehavior.setTopAndBottomOffset(-hight);
}
}
});
findViewById(R.id.text).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CoordinatorLayout.Behavior behavior =
((CoordinatorLayout.LayoutParams) appbarlayout
.getLayoutParams())
.getBehavior();
if (behavior instanceof AppBarLayout.Behavior) {
AppBarLayout.Behavior appBarLayoutBehavior = (AppBarLayout
.Behavior) behavior;
//快熟滑动到顶部
appBarLayoutBehavior.setTopAndBottomOffset(0);
}
}
});
这里 appbarlayout是AppBarLayout 的id
<com.google.android.material.appbar.AppBarLayout android:id="@+id/appbarlayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@android:color/holo_red_dark" app:elevation="0dp">