判斷到底到頂
- 自定義ScrollView控件類,代碼如下
public class myScrollView extends ScrollView { public myScrollView(Context context) { super(context); } public myScrollView(Context context, AttributeSet attributeSet) { super(context,attributeSet); } @Override protected void onScrollChanged(int l, int t, int oldl, int oldt) { View view = (View)getChildAt(getChildCount()-1); int a = view.getBottom();// //獲取View的高度和 豎直屏幕上豎直移動的距離 int b=getHeight()+getScrollY(); a=a-b; if(d==0) { //觸底事件 } if (getScrollY()==0) { //觸頂事件 } else super.onScrollChanged(l,t,oldl,oldt); } }
設置到頂到底
-
在Activity中新建一個myScrollVie
private myScrollView scrollView;
-
綁定到xml中的myScrollVie
scrollView=findViewById(R.id.scrollView);
-
設置到頂
scrollView.fullScroll(ScrollView.FOCUS_UP);
-
設置到底
scrollView.fullScroll(ScrollView.FOCUS_DOWN);