vscode关闭左边目录父文件夹粘性滚动

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
是的,可以实现随着滚动变化的粘性标题效果。实现方法是在 AppBarLayout 中的 Toolbar 中添加一个自定义的 View,用于实现随着滚动变化的效果。然后在 AppBarLayout 的 OnOffsetChangedListener 中根据滚动的距离动态改变该自定义 View 的状态,从而实现粘性标题随着滚动变化的效果。 示例代码: ```xml <androidx.coordinatorlayout.widget.CoordinatorLayout android:layout_width="match_parent" android:layout_height="match_parent"> <com.google.android.material.appbar.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <com.google.android.material.appbar.Toolbar android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" app:layout_scrollFlags="scroll|enterAlways|snap"> <LinearLayout android:id="@+id/title_container" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center"> <!-- 固定的标题内容 --> </LinearLayout> <View android:id="@+id/title_background" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/white" android:alpha="0" /> </com.google.android.material.appbar.Toolbar> </com.google.android.material.appbar.AppBarLayout> <androidx.core.widget.NestedScrollView android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior"> <!-- 滚动内容 --> </androidx.core.widget.NestedScrollView> </androidx.coordinatorlayout.widget.CoordinatorLayout> ``` ```java private int mTitleContainerHeight; private int mTitleBackgroundAlpha; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final LinearLayout titleContainer = findViewById(R.id.title_container); final View titleBackground = findViewById(R.id.title_background); // 获取标题容器的高度 titleContainer.post(new Runnable() { @Override public void run() { mTitleContainerHeight = titleContainer.getHeight(); } }); AppBarLayout appBarLayout = findViewById(R.id.app_bar_layout); appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() { @Override public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) { // 计算滚动距离占标题容器高度的百分比 float percentage = Math.abs((float) verticalOffset / (float) mTitleContainerHeight); // 根据滚动距离百分比动态改变标题背景的透明度 titleBackground.setAlpha(mTitleBackgroundAlpha * percentage); } }); } ``` 注意:在代码中需要获取标题容器的高度,因此需要在标题容器被绘制到屏幕上之后才能获取到正确的高度,因此在代码中使用了 post 方法来延迟获取高度的操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值