NestedScrollView的滑动监听目前仅限api23及以上,为了兼容低版本如下自定义方法
可以自定义NestedScrollView即可如下:
package .gfan.world.widget;import android.content.Context;import android.support.annotation.NonNull;import android.support.annotation.Nullable;import android.support.v4.widget.NestedScrollView;import android.util.AttributeSet;/*** 自定义NestedScrollView* 解决滑动监听只能在API23以上才能使用的问题** @author xiayiye5* 8月3日12:56:50*/public class XiaYiYeNestedScrollView extends NestedScrollView {private OnXiaYiYeScrollChanged mOnXiaYiYeScrollChanged;public XiaYiYeNestedScrollView(@NonNull Context context) {this(context, null);}public XiaYiYeNestedScrollView(@NonNull Context context, @Nullable AttributeSet attrs) {this(context, attrs, 0);}public XiaYiYeNestedScrollView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);}@Overrideprotected void onScrollChanged(int l, int t, int oldl, int oldt) {super.onScrollChanged(l, t, oldl, oldt);if (mOnXiaYiYeScrollChanged != null) {mOnXiaYiYeScrollChanged.onScroll(l, t, oldl, oldt);}}public void setXiaYiYeOnScrollChanged(OnXiaYiYeScrollChanged onXiaYiYeScrollChanged) {this.mOnXiaYiYeScrollChanged = onXiaYiYeScrollChanged;}public interface OnXiaYiYeScrollChanged {/*** 滑动的方法** @param left 左边* @param top上边* @param oldLeft 之前的左边* @param oldTop 之前的上边*/void onScroll(int left, int top, int oldLeft, int oldTop);}}
调用方法:
//设置滑动监听事件nestNewTopic.setXiaYiYeOnScrollChanged(new XiaYiYeNestedScrollView.OnXiaYiYeScrollChanged() {@Overridepublic void onScroll(int left, int top, int oldLeft, int oldTop) {final int[] colorValue = {0X00000000};float height = ScreenTools.dip2px(getApplicationContext(), 287) - getStatusBarHeight();int startColor = ContextCompat.getColor(mContext, R.color.color_transparent);int endColor = ContextCompat.getColor(mContext, R.color.white);if (top <= 0) {tvTitle.setTextColor(Color.WHITE);ivMainSearch.setImageResource(R.drawable.icon_main_tab_search_white);ivMainMsg.setImageResource(R.drawable.icon_main_tab_msg_white);colorValue[0] = startColor;} else if (top >= height) {//设置标题栏想要的颜色tvTitle.setTextColor(ContextCompat.getColor(parentContext, R.color.color_333));ivMainSearch.setImageResource(R.drawable.icon_main_tab_search);ivMainMsg.setImageResource(R.drawable.icon_main_tab_msg);colorValue[0] = endColor;} else {colorValue[0] = (int) argbEvaluator.evaluate(top / height, startColor, endColor);}//设置渐变背景色rlTitle.setBackgroundColor(colorValue[0]);}});
如上代码即可
感谢原博主:博主直达