最近開發(fā)需要判斷滾動(dòng)視圖視圖是否滾動(dòng)到底部阅嘶,查了一下網(wǎng)上的方案,自己總結(jié),記一下讯柔。
NestedScrollView監(jiān)聽滾動(dòng)到底部
如果要判斷滾動(dòng)到底部抡蛙,只要獲取NestedScrollView的子View的高度來對(duì)比滾動(dòng)的距離就好了。
final NestedScrollView nestedScrollView = (NestedScrollView) findViewById(R.id.view_scroll);
nestedScrollView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction()){
case MotionEvent.ACTION_MOVE:
//滾動(dòng)高度
int scrollY = view.getScrollY();
int height = view.getHeight();
int firstChildViewHeight = nestedScrollView.getChildAt(0).getMeasuredHeight();
//避免多次判斷魂迄,只在剛滾動(dòng)到底部的時(shí)候執(zhí)行
boolean tempScrollToBottom = Math.abs(scrollY + height - firstChildViewHeight) < 10;
if (tempScrollToBottom != scrollTOBottom){
scrollTOBottom = tempScrollToBottom;
if (scrollTOBottom){
Log.e("TAG", "scrolled to bottom");
}
}
break;
}
return false;
}
});
NestedScrollView監(jiān)聽滾動(dòng)到某個(gè)子View
獲取滾動(dòng)視圖的滾動(dòng)內(nèi)容粗截,可以獲取到子View后再獲取到第二級(jí)的View高度來計(jì)算。
WebView監(jiān)聽滾動(dòng)到底部
方法與NestedScrollView類似捣炬,但是在計(jì)算的時(shí)候需要添注意獲取的View高度要乘上webVeiw的縮放比熊昌。