目的只是记录,这只是一个不是很完美的解决方案:
1.重写scrollview监听滑动事件
public class MyAutoScrollView extends NestedScrollView {
public MyAutoScrollView(Context context) {
super(context);
}
public MyAutoScrollView(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public MyAutoScrollView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
if(listener != null){
//这里我只传了垂直滑动的距离
listener.onScroll(t);
}
}
//设置接口
public interface OnMyScrollListener{
void onScroll(int scrollY);
}
private OnMyScrollListener listener;
public void setOnMyScrollListener(OnMyScrollListener listener) {
this.listener = listener;
}
}
- 开启runnable 其实不该private 里面new的
/**
* 滚屏的线程
*/
private Runnable scrollRunnable = new Runnable() {
@SuppressLint("NewApi")
@Override
public void run() {
nsvBed.smoothScrollBy(0, 2);
nsvBed.postDelayed(this, 80);
}
};
3.监听滑到底部,
nsvBed.setOnMyScrollListener(new MyAutoScrollView.OnMyScrollListener() {
@Override
public void onScroll(int scrollY) {
Log.d("MainActivity", "scrollY:" + scrollY);
if (nsvBed.getChildAt(0).getHeight() - nsvBed.getHeight()
== scrollY){
nsvBed.scrollTo(0,0);
}
}
});
4.开启:
nsvBed.post(scrollRunnable);
5.关闭
nsvBed.removeCallbacks(scrollRunnable);