Android listening scrollview slides to the top and bottom

Source: Internet
Author: User

MainActivity is as follows:

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, 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 ;}};}

 

Main. xml is as follows:

<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.