[Android] ScrollView detection by scrolling to the bottom and top

Source: Internet
Author: User

1. Source


Many paging components are inherited from ScrollView. to scroll to the bottom of a page, you need to check whether the page is scrolled to the top.


2. Code


<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"    tools:context=".MainActivity" >    <ScrollView        android:id="@+id/scroll_view"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:scrollbars="vertical" >        <LinearLayout            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:background="@android:color/darker_gray"            android:orientation="vertical" >            <TextView                android:layout_width="wrap_content"                android:layout_height="2000dip"                android:text="text" />        </LinearLayout>    </ScrollView></RelativeLayout>
public class MainActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    final ScrollView view = (ScrollView) findViewById(R.id.scroll_view);    view.setOnTouchListener(new OnTouchListener() {        @Override        public boolean onTouch(View v, MotionEvent event) {            switch (event.getAction()) {            case MotionEvent.ACTION_DOWN:                break;            case MotionEvent.ACTION_MOVE:                if (v.getScrollY() <= 0) {                    Log.d("scroll view", "top");                } else if (view.getChildAt(0).getMeasuredHeight() <= v.getHeight() + v.getScrollY()) {                    Log.d("scroll view", "bottom");                    Log.d("scroll view", "view.getMeasuredHeight() = " + view.getMeasuredHeight()                        + ", v.getHeight() = " + v.getHeight()                        + ", v.getScrollY() = " + v.getScrollY()                        + ", view.getChildAt(0).getMeasuredHeight() = " + view.getChildAt(0).getMeasuredHeight());                }                break;            default:                break;            }            return false;} });    }}


3. Description

There are a lot of times to check for scrolling. Here we use MotionEvent. ACTION_MOVE of onTouch to detect rolling outside ScrollView. If it inherits ScrollView, it can be detected during other callbacks.

V. getScrollY () indicates the length of the scrollview. V. getHeight () indicates the height of the scrollview. View. getChildAt (0). getMeasuredHeight () is the height of the content. View. getMeasureHeight () cannot be used here ().

View. getMeasuredHeight () = 1038

V. getHeight () = 1038

V. getScrollY () = 2962

View. getChildAt (0). getMeasuredHeight () = 4000

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.