1. 程式人生 > >Android中自定義ScrollView的滑動監聽事件

Android中自定義ScrollView的滑動監聽事件

專案結構:


1.LazyScrollView類(自定義ScrollView
package android.zhh.com.myapplicationscrollview;

/**
 * Created by sky on 2017/3/19.
 */

import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ScrollView;

/**
 * Created by sky on 2017/3/17.
 */
public class LazyScrollView extends ScrollView {
    private static final long DELAY = 100;

    private int currentScroll;

    private Runnable scrollCheckTask;

    /**
     * @param context
     */
    public LazyScrollView(Context context) {
        super(context);
        init();
    }

    /**
     * @param context
     * @param attrs
     */
    public LazyScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    /**
     * @param context
     * @param attrs
     * @param defStyle
     */
    public LazyScrollView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private void init() {
        scrollCheckTask = new Runnable() {
            @Override
            public void run() {
                int newScroll = getScrollY();
                if (currentScroll == newScroll) {
                    if (onScrollListener != null) {
                        onScrollListener.onScrollStopped();
                    }
                } else {
                    if (onScrollListener != null) {
                        onScrollListener.onScrolling();
                    }
                    currentScroll = getScrollY();
                    postDelayed(scrollCheckTask, DELAY);
                }
            }
        };
        setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    currentScroll = getScrollY();
                    postDelayed(scrollCheckTask, DELAY);
                }
                return false;
            }
        });
    }

    public interface OnScrollListener {
        public void onScrollChanged(int x, int y, int oldX, int oldY);

        public void onScrollStopped();

        public void onScrolling();
    }

    private OnScrollListener onScrollListener;

    /**
     * @param onScrollListener
     */
    public void setOnScrollListener(OnScrollListener onScrollListener) {
        this.onScrollListener = onScrollListener;
    }

    @Override
    protected void onScrollChanged(int x, int y, int oldX, int oldY) {
        super.onScrollChanged(x, y, oldX, oldY);
        if (onScrollListener != null) {
            onScrollListener.onScrollChanged(x, y, oldX, oldY);
        }
    }

    /**
     * @param child
     * @return
     */
    public boolean isChildVisible(View child) {
        if (child == null) {
            return false;
        }
        Rect scrollBounds = new Rect();
        getHitRect(scrollBounds);
        return child.getLocalVisibleRect(scrollBounds);
    }

    /**
     * @return
     */
    public boolean isAtTop() {
        return getScrollY() <= 0;
    }

    /**
     * @return
     */
    public boolean isAtBottom() {
        return getChildAt(getChildCount() - 1).getBottom() + getPaddingBottom() == getHeight() + getScrollY();
    }
}
2activity_main.xml(佈局檔案中引用
<LinearLayout 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"
    android:orientation="vertical"
    >
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <android.zhh.com.myapplicationscrollview.LazyScrollView
            android:id="@+id/myScrollView"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                >
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="30dp"
                    android:text="我是翟浩浩"
                    android:gravity="center"
                    />
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="30dp"
                    android:text="我是翟浩浩"
                    android:gravity="center"
                    />
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="30dp"
                    android:text="我是翟浩浩"
                    android:gravity="center"
                    />
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="30dp"
                    android:text="我是翟浩浩"
                    android:gravity="center"
                    />
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="30dp"
                    android:text="我是翟浩浩"
                    android:gravity="center"
                    />
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="30dp"
                    android:text="我是翟浩浩"
                    android:gravity="center"
                    />
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="30dp"
                    android:text="我是翟浩浩"
                    android:gravity="center"
                    />
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="30dp"
                    android:text="我是翟浩浩"
                    android:gravity="center"
                    />
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="30dp"
                    android:text="我是翟浩浩"
                    android:gravity="center"
                    />
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="30dp"
                    android:text="我是翟浩浩"
                    android:gravity="center"
                    />
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="30dp"
                    android:text="我是翟浩浩"
                    android:gravity="center"
                    />
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="30dp"
                    android:text="我是翟浩浩"
                    android:gravity="center"
                    />
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="30dp"
                    android:text="我是翟浩浩"
                    android:gravity="center"
                    />
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="30dp"
                    android:text="我是翟浩浩"
                    android:gravity="center"
                    />
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="30dp"
                    android:text="我是翟浩浩"
                    android:gravity="center"
                    />
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="30dp"
                    android:text="我是翟浩浩"
                    android:gravity="center"
                    />
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="30dp"
                    android:text="我是翟浩浩"
                    android:gravity="center"
                    />
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="30dp"
                    android:text="我是翟浩浩"
                    android:gravity="center"
                    />
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="30dp"
                    android:text="我是翟浩浩"
                    android:gravity="center"
                    />
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="30dp"
                    android:text="我是翟浩浩"
                    android:gravity="center"
                    />
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="30dp"
                    android:text="我是翟浩浩"
                    android:gravity="center"
                    />
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="30dp"
                    android:text="我是翟浩浩"
                    android:gravity="center"
                    />
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="30dp"
                    android:text="我是翟浩浩"
                    android:gravity="center"
                    />
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="30dp"
                    android:text="我是翟浩浩"
                    android:gravity="center"
                    />
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="30dp"
                    android:text="我是翟浩浩"
                    android:gravity="center"
                    />
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="30dp"
                    android:text="我是翟浩浩"
                    android:gravity="center"
                    />
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="30dp"
                    android:text="我是翟浩浩"
                    android:gravity="center"
                    />
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="30dp"
                    android:text="我是翟浩浩"
                    android:gravity="center"
                    />
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="30dp"
                    android:text="我是翟浩浩"
                    android:gravity="center"
                    />
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="30dp"
                    android:text="我是翟浩浩"
                    android:gravity="center"
                    />
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="30dp"
                    android:text="我是翟浩浩"
                    android:gravity="center"
                    />
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="30dp"
                    android:text="我是翟浩浩"
                    android:gravity="center"
                    />
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="30dp"
                    android:text="我是翟浩浩"
                    android:gravity="center"
                    />
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="30dp"
                    android:text="我是翟浩浩"
                    android:gravity="center"
                    />
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="30dp"
                    android:text="我是翟浩浩"
                    android:gravity="center"
                    />
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="30dp"
                    android:text="我是翟浩浩"
                    android:gravity="center"
                    />
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="30dp"
                    android:text="我是翟浩浩"
                    android:gravity="center"
                    />
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="30dp"
                    android:text="我是翟浩浩"
                    android:gravity="center"
                    />
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="30dp"
                    android:text="我是翟浩浩"
                    android:gravity="center"
                    />
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="30dp"
                    android:text="我是翟浩浩"
                    android:gravity="center"
                    />
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="30dp"
                    android:text="我是翟浩浩"
                    android:gravity="center"
                    />
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="30dp"
                    android:text="我是翟浩浩"
                    android:gravity="center"
                    />
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="30dp"
                    android:text="我是翟浩浩"
                    android:gravity="center"
                    />
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="30dp"
                    android:text="我是翟浩浩"
                    android:gravity="center"
                    />
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="30dp"
                    android:text="我是翟浩浩"
                    android:gravity="center"
                    />
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="30dp"
                    android:text="我是翟浩浩"
                    android:gravity="center"
                    />


            </LinearLayout>
        </android.zhh.com.myapplicationscrollview.LazyScrollView>

    </RelativeLayout>

</LinearLayout>

3MainActivity(呼叫監聽事件)
package android.zhh.com.myapplicationscrollview;

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
//  初始化自定義的ScrollView
    private LazyScrollView myScrollView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myScrollView = (LazyScrollView)findViewById(R.id.myScrollView);
//      自定義的ScrollView的滑動監聽事件
        myScrollView.setOnScrollListener(new LazyScrollView.OnScrollListener() {
            @Override
            public void onScrollChanged(int x, int y, int oldX, int oldY) {
                Log.e("@", "x:" + oldX + "->" + x + ", y:" + oldY + "->" + y);
            }

            @Override
            public void onScrollStopped() {
                if (myScrollView.isAtTop()) {
                    Toast.makeText(MainActivity.this, "Stopped at top", Toast.LENGTH_SHORT).show();
                } else if (myScrollView.isAtBottom()) {
                    Toast.makeText(MainActivity.this, "Stopped at bottom", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(MainActivity.this, "Stopped", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onScrolling() {
                Log.e("@", "scrolling...");
            }
        });
    }

}

原始碼下載:

相關推薦

Android定義ScrollView滑動事件

專案結構: 1.LazyScrollView類(自定義ScrollView) package android.zhh.com.myapplicationscrollview; /** * Created by sky on 2017/3/19. */ impor

Android定義ScrollView滑動事件,並在滑動時漸變標題欄背景顏色

效果圖 滑動前: 滑動中: 滑動到底部: 專案結構 ObservableScrollView package com.jukopro.titlebarcolor; import android.content.Context; import android.u

jquery 定義input輸入事件

網上一段JS,考來的,自定義監聽: $.event.special.valuechange = { teardown: function (namespaces) { $(this).unbind('.valuechange')

AndroidPreference的使用以及事件分析

                  在Android系統原始碼中,絕大多數應用程式的UI佈局採用了Preference的佈局結構,而不是我們平時在模擬器中構建應用程式時使用的View佈局結構,例如,Setting模組中佈局。當然,凡事都有例外,FMRadio應用程式中

Android ScrollView滑動

     因為專案裡用到了ScrollView, 並需要實現類似於ListView的滑動監聽回撥,所以自定義了一套實現方式支援這些事件, 基本滿足了業務需求;public interface OnMyScrollListener { int SCROLL_STATE_

Android定義滑動選中控制元件WheelView

WheelView a great functional custom WheelView with demo in dialog and bottomDialog,android 滾動選擇控制元件,滾動選擇器 ========= How to

Android學習之定義TextWatcher來文字最大輸入字數

開發中有種很可能會遇到的需求就是限制EditText的文字輸入字數,例如微博就限制140字,如果只是限制輸入的字數的話很簡單,EditText有個屬性叫android:maxLength,設定140就

android定義的dialog的EditText無法彈出輸入法解決方案

dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);//彈出輸入法,並且寫在show()方法之後。 解決Dialog 消失,輸入法不消失的問題: 參考:https://blog.csd

Android定義DatePicker

先看一下效果 看這個圖很顯然就是一個DatePicker和一個TimePicker組合來實現的 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.a

Android定義Dialog樣式

public class MyMiddleDialog extends Dialog { private Context context; public MyMiddleDialog(Context context) { sup

Android定義TabLayout指示器長度

效果圖: MainActivity.java檔案 public class MainActivity extends AppCompatActivity { @Override protected void onCreate(@Nullabl

Android定義ViewGroup使每行元件數量不確定,並拿到選中資料

1先看效果圖 2專案目錄 3在定義控制元件FlowTagGroup package android.zhh.com.myviewgroup; /** * Created by sky on 2017/3/10. */ import android.conten

Android定義ProgressBar的樣式

如果想快速獲取水平進度條顯示操作,直接進入第四步和第六步操作就可以了!! 首先可以去sdk中檢視 sdk1\platforms\android-23\data\res\values,中的sty

Android定義底部彈出框ButtomDialog

先看看效果和你要的是否一樣 一 、先來配置自定義控制元件需要的資源。 1.在res資料夾下建立一個anim資料夾並建立兩個slide_in_bottom.xml、slide_out_bottom.xml檔案,負責彈框進出動畫。 <?xml version="1.0" enco

Android定義控制元件SegmentedGroup

GitHub:https://github.com/Kaopiz/android-segmented-control 一 、新增依賴 implementation 'info.hoang8f:android-segmented:1.0.6' 二、佈局中使用 <info.hoan

Android定義SeekBar的樣式

有時候原生的SeekBar太醜了,已經滿足不了我們的效果,需要我們自定義樣式。 第一步:在drawable裡建立一個xml檔案 <?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://

Android定義頂部狀態列顏色

public class StatusBarUtils { public static void setWindowStatusBarColor(Activity activity, int c

Android定義drawable資源實現佈局的圓角邊框效果

佈局的圓角邊框效果圖如下所示: 如上圖紅色標註的部分就是一個圓角邊框效果的自定義搜尋框。 實現起來很簡單,讓佈局(Relativelayout或者LinearLayout)的background屬性引用自定義的drawable資源即可。 andro

android定義畫布Canvas的實現

一、要求:1.畫布繪製控制元件的方法,控制元件應該是一個可以自定義的;2.畫布是可以縮放,且提供一個縮放的方法供外使用;3.控制元件之間連線的方法;4.畫布縮放之後手勢滑動的識別實現; 二、在github裡面種找到了一個類似度挺高的開源專案: github中的第三方的開源專

Android定義SeekBar背景顏色,進度條顏色,滑塊圖片

目錄 Android SeekBar常見問題 在使用Android Seekbar大家可能經常遇到下面這幾個問題: 如何設定Seekbar進度條的高度? 如何設定滑塊的樣式? 如何設定進度條的顏色和背景顏色? 接下來,針對這三個問題我會