Package cn. testscrollview; import android. OS. bundle; import android. view. motionEvent; import android. view. view; import android. view. view. onTouchListener; import android. widget. scrollView; import android. app. activity;/*** Demo Description: * listens to ScrollView slides to the top and bottom ** Note: * 1 mScrollView. getChildAt (0 ). getMeasuredHeight () indicates the height occupied by ScrollView. the height of the ScrollView content. A part of the content is usually visible only after sliding. The height of this part is also included in * mScrollView. getChildAt (0 ). ** 2 view in getMeasuredHeight. getScrollY indicates: * The height at the top of the ScrollView that has been slid out ** 3 view. getHeight () indicates the visible height of * ScrollView **/public class MainActivity extends Activity {private ScrollView mScrollView; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); init ();} private void init () {mScrollView = (ScrollView) findViewById (R. id. scrollView); mScrollView. setOnTouchListener (new TouchListenerImpl ();} private class TouchListenerImpl implements OnTouchListener {@ Overridepublic boolean onTouch (View view, MotionEvent motionEvent) {switch (motionEvent. getAction () {case MotionEvent. ACTION_DOWN: break; case MotionEvent. ACTION_MOVE: int scrollY = view. getScrollY (); int height = view. getHeight (); int scrollViewMeasuredHeight = mScrollView. getChildAt (0 ). getMeasuredHeight (); if (scrollY = 0) {System. out. println ("slide to the top view. getScrollY () = "+ scrollY);} if (scrollY + height) = scrollViewMeasuredHeight) {System. out. println ("slide to the bottom scrollY =" + scrollY); System. out. println ("sliding to the bottom height =" + height); System. out. println ("sliding to the bottom of scrollViewMeasuredHeight =" + scrollViewMeasuredHeight);} break; default: break;} return false ;}};}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <ScrollView android:id="@+id/scrollView" android:layout_width="fill_parent" android:layout_height="wrap_content" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello_world"/> </ScrollView></RelativeLayout>