coordingLayout嵌套RecycleView滑动改变搜索标题渐变

纠结了好久滑动事件监听是coordingLayout,coordingLayout滑动监听onScrollChanged,发现一直没有改变,又改变监听RecycleView,是可以改变,但是达不到预期效果,RecycleView总是在coordingLayout滑到界面顶端才会有高度变化。

最后才发现coordingLayout中的AppBarLayout可以实时监听到高度的变化.

代码如下:

 private int height = 400;// 滑动开始变色的高,真实项目中此高度是由广告轮播或其他首页view高度决定

bindingView.appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                LogUtils.e("=======AppBarLayout======" + Math.abs(verticalOffset));
                if (Math.abs(verticalOffset) <= 0) {   //设置标题的背景颜色
                    bindingView.layoutHomeSearchHeader.rltySearchHeader.setBackgroundColor(Color.argb((int) 0, 248, 247, 247));
                    bindingView.layoutHomeSearchHeader.edtSearch.setBackground(getResources().getDrawable(R.drawable.shape_edittext_search_bg_default));
                    bindingView.layoutHomeSearchHeader.rltySearchHeader.setBackgroundResource(R.color.transparent);
                    bindingView.layoutHomeSearchHeader.tvSelect.setTextColor(getResources().getColor(R.color.colorWhite));
                    bindingView.layoutHomeSearchHeader.edtSearch.setTextColor(getResources().getColor(R.color.colorWhite));
                    bindingView.layoutHomeSearchHeader.ivHomeRefresh.setBackground(getResources().getDrawable(R.drawable.ic_home_refresh_default));
                    Drawable drawable = getResources().getDrawable(R.drawable.ic_home_select_default);
                    drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
                    bindingView.layoutHomeSearchHeader.tvSelect.setCompoundDrawables(null, null, drawable, null);
                } else if (Math.abs(verticalOffset) > 0 && Math.abs(verticalOffset) <= height) { //滑动距离小于banner图的高度时,设置背景和字体颜色颜色透明度渐变
                    float scale = (float) Math.abs(verticalOffset) / height;
                    float alpha = (255 * scale);
                    bindingView.layoutHomeSearchHeader.rltySearchHeader.setBackgroundColor(Color.argb((int) alpha, 248, 247, 247));
                    if (alpha >= 127) {
                        setUIScrolled();
                    }
                } else {
                    bindingView.layoutHomeSearchHeader.rltySearchHeader.setBackgroundColor(Color.argb((int) 255, 248, 247, 247));
                    setUIScrolled();
                }
            }
        });

private void setUIScrolled() {
        bindingView.layoutHomeSearchHeader.edtSearch.setBackground(getResources().getDrawable(R.drawable.shape_edittext_search_bg_scrolled));
        bindingView.layoutHomeSearchHeader.tvSelect.setTextColor(getResources().getColor(R.color.gray999999));
        bindingView.layoutHomeSearchHeader.edtSearch.setTextColor(getResources().getColor(R.color.gray999999));
        bindingView.layoutHomeSearchHeader.ivHomeRefresh.setBackground(getResources().getDrawable(R.drawable.ic_home_refresh_scrolled));
        Drawable drawable = getResources().getDrawable(R.drawable.ic_home_select_scrolled);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        bindingView.layoutHomeSearchHeader.tvSelect.setCompoundDrawables(null, null, drawable, null);
    }
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorWhite">

        <cn.nanmi.yibidding.widget.MyCoordinatorLayout
            android:id="@+id/coordingLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <com.jcodecraeer.xrecyclerview.XRecyclerView
                android:id="@+id/xrv_home"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                app:layout_behavior="@string/appbar_scrolling_view_behavior" />

            <android.support.design.widget.AppBarLayout
                android:id="@+id/appBarLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">


                <com.youth.banner.Banner
                    android:id="@+id/view_banner"
                    android:layout_width="match_parent"
                    android:layout_height="190dp"
                    android:minHeight="45dp"
                    app:image_scale_type="center_crop"
                    app:indicator_drawable_selected="@drawable/banner_red"
                    app:indicator_drawable_unselected="@drawable/banner_grey"
                    app:indicator_height="9dp"
                    app:indicator_margin="3dp"
                    app:indicator_width="9dp"
                    app:is_auto_play="true"
                    app:layout_scrollFlags="scroll|exitUntilCollapsed" />


                <include
                    android:id="@+id/layout_select_tab"
                    layout="@layout/layout_home_secect_button" />

                <View style="@style/divider_gray_setting" />
            </android.support.design.widget.AppBarLayout>


        </cn.nanmi.yibidding.widget.MyCoordinatorLayout>

        <include
            android:id="@+id/layout_home_search_header"
            layout="@layout/view_home_search_header"
            android:visibility="visible" />

        <!--<RelativeLayout-->
        <!--android:layout_width="match_parent"-->
        <!--android:layout_height="45dp"-->
        <!--android:background="@color/colorYello">-->

        <!--<TextView-->
        <!--android:layout_width="wrap_content"-->
        <!--android:layout_height="wrap_content"-->
        <!--android:layout_centerInParent="true"-->
        <!--android:text="@string/app_name"-->
        <!--android:textColor="@color/colorWhite"-->
        <!--android:textSize="18sp" />-->
        <!--</RelativeLayout>-->

    </RelativeLayout>
</layout>






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值