Android监听ScrollView或NestedScrollView滑动到顶部和底部

先看几个概念:
1.ScrollView内容的高度,包括需要滑动才可见到的部分:dydmScrollView.getChildAt(0).getMeasuredHeight();
2.view.getScrollY:ScrollView顶端已经滑出去的高度;
3.view.getHeight():ScrollView的可见高度。

实例代码:

import android.os.Bundle;
import android.support.v4.widget.NestedScrollView;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ScrollView;
import android.app.Activity;


public class MainActivity extends Activity {
    private ScrollView dydmScrollView;//或者NestedScrollView

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        init();
    }

    private void init() {
        dydmScrollView = (ScrollView) findViewById(R.id.scrollView);
        dydmScrollView.setOnTouchListener(new TouchListenerImpl());
    }

    private class TouchListenerImpl implements OnTouchListener {
        @Override
        public 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 = dydmScrollView.getChildAt(0).getMeasuredHeight();
                    //顶部
                    if (scrollY == 0) {
                        Log.d("顶部", "" + scrollY);
                    }
                    //底部
                    if ((scrollY + height) == scrollViewMeasuredHeight) {
                        Log.d("底部 scrollY=", "" + scrollY);
                        Log.d("底部 height=", "" + height);
                        Log.d("底部 scrollViewMeasuredHeight=", "" + scrollViewMeasuredHeight);
                    }
                    break;

                default:
                    break;
            }
            return false;
        }

    }
}

布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/scrollView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:fadingEdge="vertical">

        <ListView
            android:id="@+id/id_listview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:cacheColorHint="#00000000"
            android:divider="#eeeeee"
            android:dividerHeight="0.5dp"
            android:fadingEdge="none"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:listSelector="#00000000" />

    </android.support.v4.widget.NestedScrollView>

    <!--或者是下面这种滚动视图ScrollView-->
    <!--<ScrollView-->
    <!--android:id="@+id/scrollView"-->
    <!--android:layout_width="fill_parent"-->
    <!--android:layout_height="wrap_content"-->
    <!--android:layout_weight="1"-->
    <!--android:fadingEdge="vertical">-->

    <!--<ListView-->
    <!--android:id="@+id/id_listview"-->
    <!--android:layout_width="match_parent"-->
    <!--android:layout_height="wrap_content"-->
    <!--android:layout_weight="1"-->
    <!--android:dividerHeight="0.5dp"-->
    <!--android:divider="#eeeeee"-->
    <!--android:cacheColorHint="#00000000"-->
    <!--android:fadingEdge="none"-->
    <!--android:focusable="true"-->
    <!--android:focusableInTouchMode="true"-->
    <!--android:listSelector="#00000000"/>-->
    <!---->
    <!--</ScrollView>-->
</LinearLayout>

发表回复

后才能评论