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);
    }
布局文件:



    

        

            

            


                


                

                
            


        

        

        
        
        
        

        
        
        
        
        
        
        
        

    


 
  

 
  

你可能感兴趣的:(安卓进阶)