ScrollView改变标题栏、状态栏透明度并改变状态栏字体颜色----scrollview滑动监听还有点问题,,,

先上个动图:

这里写图片描述

UI结构

这里写图片描述

代码实现:

xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <!--刷新父控件-->
    <com.scwang.smartrefresh.layout.SmartRefreshLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <!--帧布局父控件-->
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
            <!--自定义scrollview-->
            <app.myap.eyecloudtest.utils.CustomScrollView
                android:id="@+id/scrollView_main"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:orientation="vertical">
                        <com.daimajia.slider.library.SliderLayout
                            android:id="@+id/slider"
                            android:layout_width="match_parent"
                            android:layout_height="260dp"
                            custom:auto_cycle="true"
                            custom:indicator_visibility="visible"
                            custom:pager_animation="FlipPage"
                            custom:pager_animation_span="1100" />

                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="500dp"
                            android:background="#00b7ee"

                            android:text="hahha" />

                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="500dp"
                            android:layout_marginTop="50dp"
                            android:background="#00b7ee"
                            android:text="hahha" />
                    </LinearLayout>
            </app.myap.eyecloudtest.utils.CustomScrollView>
            <!--标题栏及状态栏布局-->
            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="70dp">

                <LinearLayout
                    android:id="@+id/header_view"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="#ffffff"
                    android:orientation="vertical">

                </LinearLayout>

                <RelativeLayout
                    android:id="@+id/ll_m"
                    android:layout_width="match_parent"
                    android:layout_height="60dp"
                    android:layout_gravity="bottom"
                    android:orientation="horizontal">

                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentLeft="true"
                        android:layout_centerVertical="true"
                        android:layout_marginLeft="20dp"
                        android:src="@drawable/icon_main_add" />

                    <TextView
                        android:id="@+id/header_title"
                        android:layout_width="wrap_content"
                        android:layout_height="match_parent"
                        android:layout_centerInParent="true"
                        android:gravity="center_vertical"
                        android:text="眼云智家"
                        android:textColor="#ffffff"
                        android:textSize="20sp" />

                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentRight="true"
                        android:layout_centerVertical="true"
                        android:layout_marginRight="20dp"
                        android:src="@drawable/icon_main_ma" />
                </RelativeLayout>
            </FrameLayout>
        </FrameLayout>
    </com.scwang.smartrefresh.layout.SmartRefreshLayout>
</FrameLayout>

java代码,主要用到了一个依赖

   //刷新控件
    compile 'com.scwang.smartrefresh:SmartRefreshLayout:1.1.0-alpha-2'
    //状态栏管理
    compile 'com.jaeger.statusbarutil:library:1.5.1'
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        StatusBarUtil.setTranslucentForImageViewInFragment(getActivity(), 0, null);
        StatusBarUtil.setDarkMode(getActivity());//白色
        //添加Bannner
        addBanner();
        //添加滑动监听
        addScrollViewListener();
    }
 /**
     * 添加滑动监听
     * (重点在这)
     */
    private void addScrollViewListener() {
        headerView.setAlpha(0);
        final int h = dpTopx(100);
        scrollViewMain.setOnScrollChangeListener(new CustomScrollView.OnScrollChangeListener() {
            @Override
            public void onScrollChanged(CustomScrollView scrollView, int l, int y, int oldl, int oldt) {
                //设置标题栏的透明度
                if (y > 0 && y <= h) { //滑动距离小于banner图的高度时,设置背景和字体颜色颜色透明度渐变
                    float scale = (float) y / h;
                    headerView.setAlpha(scale);
                }
                //设置状态栏颜色变化(只支持魅族和小米)
                //设置标题栏的子view字体等变化
                if (y < 30) {
                    StatusBarUtil.setDarkMode(getActivity());//白色
                    headerTitle.setTextColor(Color.WHITE);
                } else {
                    StatusBarUtil.setLightMode(getActivity());//黑色
                    headerTitle.setTextColor(Color.BLACK);
                }
            }
        });
    }
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现这个效果可以采用两种方案: 1. 使用 CoordinatorLayout 实现 CoordinatorLayout 是一个非常强大的布局容器,它可以协调多个子 View 的交互行为。在这个场景下,我们可以将标题栏作为 CoordinatorLayout 的直接子 View,将 ScrollView 作为标题栏的兄弟 View,然后使用 app:layout_behavior 属性指定标题栏的行为为 AppBarLayout.ScrollingViewBehavior,这样当 ScrollView 滑动时,标题栏就会自动滑动。 示例代码如下: ```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="?attr/actionBarSize"> <com.google.android.material.appbar.MaterialToolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" app:title="My Title" /> </com.google.android.material.appbar.AppBarLayout> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior"> <!-- Your content here --> </ScrollView> </androidx.coordinatorlayout.widget.CoordinatorLayout> ``` 需要注意的是,这种方案需要使用 AndroidX 库中的 CoordinatorLayout 和 Material Design 组件库中的 AppBarLayout 和 MaterialToolbar。 2. 使用自定义 Behavior 实现 如果你不想使用 CoordinatorLayout,或者你需要实现一些比较特殊的效果,那么可以考虑使用自定义 Behavior 实现。具体来说,我们可以定义一个 Behavior,继承自 AppBarLayout.ScrollingViewBehavior,并重写 onNestedPreScroll 方法,在 ScrollView 滑动之前处理标题栏滑动。 示例代码如下: ```java public class MyBehavior extends AppBarLayout.ScrollingViewBehavior { public MyBehavior(Context context, AttributeSet attrs) { super(context, attrs); } @Override public boolean onNestedPreScroll(CoordinatorLayout coordinatorLayout, View child, View target, int dx, int dy, int[] consumed) { // 计算标题栏需要滑动的距离 int distance = Math.min(child.getHeight(), dy); // 滑动标题栏 child.setTranslationY(-distance); // 更新 consumed 数组,表示已经消耗了滑动距离 consumed[1] = distance; return true; } } ``` 然后在布局文件中将标题栏的 Behavior 设置为我们定义的 MyBehavior: ```xml <com.google.android.material.appbar.AppBarLayout android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" app:layout_behavior=".MyBehavior"> <com.google.android.material.appbar.MaterialToolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" app:title="My Title" /> </com.google.android.material.appbar.AppBarLayout> ``` 需要注意的是,这种方案需要手动处理标题栏滑动,因此需要在 onNestedPreScroll 方法中计算标题栏需要滑动的距离,并使用 setTranslationY 方法滑动标题栏。同时,需要更新 consumed 数组,表示已经消耗了滑动距离,这样才能确保滑动事件正确地传递给 ScrollView

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值