仿京东淘宝商品详情页title滑动隐藏

话不多说直接上图上代码

1.一些布局代码

// 从这货拿到scorll
<android.support.v4.widget.NestedScrollView
        android:id="@+id/nsv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:overScrollMode="never">
        
        <LinearLayout 
        		xxx...>
        		
				// 根据这货移动的位置显示隐藏title
                <com.youth.banner.Banner
                    android:id="@+id/banner"
                    android:layout_width="match_parent"
                    android:layout_height="400dp"/>

        </LinearLayout>

  </android.support.v4.widget.NestedScrollView>
    

2.一些监听代码


// 这个banner就是布局里的那个
ViewTreeObserver viewTreeObserver = banner.getViewTreeObserver();
viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @TargetApi(Build.VERSION_CODES.M)
            @Override
            public void onGlobalLayout()
            {
            // 这个nsv就是NestedScrollView
                nsv.setOnScrollChangeListener(new View.OnScrollChangeListener() {
                    @Override
                    public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY)
                    {
                    	// 拿到一个height
                        height = banner.getHeight() - Utils.dip2px(44);
                        mScrollY = scrollY;
                        changeTitle(scrollY);
                    }
                });
            }
        });

3.一些重点代码

 private void changeTitle(int scrollY)
    {
        if (scrollY <= 0)
        {
            //设置标题的背景颜色
            llTitle.setBackgroundColor(Color.argb(0, 255, 255, 255));
            tvTitle.setTextColor(Color.argb(0, 1, 24, 28));
            ivLeft.setAlpha(1f);
            ivRight.setAlpha(1f);
            ivLeft.setImageResource(R.mipmap.icon_back_goods);
            ivRight.setImageResource(mIsCollect ? R.mipmap.icon_collect_red1 : R.mipmap.icon_collect1);

        } else if (scrollY > 1 && scrollY <= height)
        {
            // 滑动距离小于图的高度时,设置背景和字体颜色颜色透明度渐变
            float scale = (float) scrollY / height;
            float alpha = (255 * scale);
            llTitle.setBackgroundColor(Color.argb((int) alpha, 255, 255, 255));
            tvTitle.setTextColor(Color.argb((int) alpha, 1, 24, 28));

            if (scrollY <= height / 2)
            {
                float scale2 = (float) scrollY / ((float) height / 2);
                ivLeft.setAlpha(1f - scale2);
                ivRight.setAlpha(1f - scale2);
                if ((scrollY >= height / 2 - 10) && scrollY <= height / 2)
                {
                    // 减少set
                    ivLeft.setImageResource(R.mipmap.icon_back_goods);
                    ivRight.setImageResource(mIsCollect ? R.mipmap.icon_collect_red1 : R.mipmap.icon_collect1);
                }
            } else
            {
                float scale3 = ((float) scrollY - ((float) (height / 2))) / ((float) height / 2);
                ivLeft.setAlpha(scale3);
                ivRight.setAlpha(scale3);
                if ((scrollY >= height / 2 + 10) && scrollY <= height)
                {
                    // 减少set
                    ivLeft.setImageResource(R.mipmap.icon_back_goods_black);
                    ivRight.setImageResource(mIsCollect ? R.mipmap.icon_collect_red2 : R.mipmap.icon_collect2);
                }
            }

        } else
        {
            // 滑动到图下面设置普通颜色
            llTitle.setBackgroundColor(Color.argb(255, 255, 255, 255));
            tvTitle.setTextColor(Color.argb(255, 1, 24, 28));
            ivLeft.setAlpha(1f);
            ivRight.setAlpha(1f);
            ivLeft.setImageResource(R.mipmap.icon_back_goods_black);
            ivRight.setImageResource(mIsCollect ? R.mipmap.icon_collect_red2 : R.mipmap.icon_collect2);
        }
    }




如有问题请指正

你可能感兴趣的:(title)