Android listening ScrollView slides to the top and bottom

Source: Internet
Author: User

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>

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.