private int height = 620;// 滑动开始变色的高,真实项目中此高度是由广告轮播或其他首页view高度决定
private int overallXScroll = 0;
private void initHideShowTitle() {
mBindingView.xRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
overallXScroll = overallXScroll + dy;// 累加y值 解决滑动一半y值为0
if (overallXScroll <= 0) { //设置标题的背景颜色 透明
mBindingView.llTitle.setBackgroundColor(Color.argb((int) 0, 255, 255, 255));
} else if (overallXScroll > 0 && overallXScroll <= height) { //滑动距离小于banner图的高度时,设置背景和字体颜色颜色透明度渐变
float scale = (float) overallXScroll / height;
float alpha = (255 * scale);
mBindingView.llTitle.setBackgroundColor(Color.argb((int) alpha, 255, 255, 255)); 由透明变白过程
} else {
mBindingView.llTitle.setBackgroundColor(Color.argb((int) 255, 255, 255, 255)); 变到白色
}
}
});
}