RecycleView滚动监听
实测可用方法:
mRecyclerView.addOnScrollListener(newRecyclerView.OnScrollListener() {
privateinttotalDy =0;
@Override
publicvoidonScrolled(RecyclerView recyclerView,intdx,intdy) {
totalDy -= dy;
}
});
使用场景: 对RecycleView滚动距离进行监听,动态改变 ActionBar透明度 ,完成渐变标题栏
具体实现代码:
viewHolder.getRecyclerView().setOnScrollListener(newRecyclerView.OnScrollListener() {
@Override
public voidonScrollStateChanged(RecyclerView recyclerView, intnewState) {
super.onScrollStateChanged(recyclerView,newState);
}
@Override
public voidonScrolled(RecyclerView recyclerView, intdx, intdy) {
/*计算透明度 算法说明: if条件,450是 规定在高度0-450之间进行变动 alpha不透明时值为255 ,值越小越透 增加if else判断,提高稳定性*/
d-= dy;
if(d<450) {
floatscale = (float) (-d) /450;
if(scale >1) {
scale =1;
}
floatalpha = (255* scale);
Log.i("--->>>","onScrolled:dy "+ dy +"---d总距离"+d+"--透明度"+ alpha);
//只是layout背景透明(仿知乎滑动效果)55c1f5 Color.argb((int) alpha, 0xfd, 0x91, 0x5b)
viewHolder.getBarLinearLayout().setBackgroundColor(Color.argb((int) alpha,0x55,0xc1,0xf5));
}else{
Log.i("--->>>","onScrolled:dy距离大于450");
viewHolder.getBarLinearLayout().setBackgroundColor(Color.argb(255,0x55,0xc1,0xf5));
}
}
});
参考:http://blog.csdn.net/wangbaochu/article/details/49446469