欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 人文社科 > 生活经验 >内容正文

生活经验

CoordinatorLayout 和 AppBarLayout 实现的局部点击按钮实现滑动某一个固定的距离

发布时间:2023/11/27 生活经验 63 豆豆
生活随笔 收集整理的这篇文章主要介绍了 CoordinatorLayout 和 AppBarLayout 实现的局部点击按钮实现滑动某一个固定的距离 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

实现的效果如下:

如果需要的只是折叠效果 可以看这遍文章点击跳转!!!!!!!!!!!!!!!!!!!!

具体代码如下

    findViewById(R.id.tv).setOnClickListener(new View.OnClickListener() {@Overridepublic 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() {@Overridepublic 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.AppBarLayoutandroid:id="@+id/appbarlayout"android:layout_width="match_parent"android:layout_height="wrap_content"android:background="@android:color/holo_red_dark"app:elevation="0dp">

总结

以上是生活随笔为你收集整理的CoordinatorLayout 和 AppBarLayout 实现的局部点击按钮实现滑动某一个固定的距离的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。