Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 中監聽 ScrollView 滑動事件的實現

編輯:關於 Android 程式設計

有時候我們需要實時掌握 ScrollView 的滑動情況,例如滑動了多少距離、是否已經滑到頂部或底部。遺憾的是 SDK 並沒有直接提供對外的方法,只保留了一個 protected 的 onScrollChanged(),外部無法調用。解決思路很簡單:自定義一個 ScrollView,並通過接口把這個回調暴露出來。

一、定義回調接口

package com.example.demo1;

public interface ScrollViewListener {
    void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy);
}

二、自定義 ObservableScrollView

重寫 ScrollView,並在 onScrollChanged 中把事件轉發給監聽器:

package com.example.demo1;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ScrollView;

public class ObservableScrollView extends ScrollView {

    private ScrollViewListener scrollViewListener = null;

    public ObservableScrollView(Context context) {
        super(context);
    }

    public ObservableScrollView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public ObservableScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void setScrollViewListener(ScrollViewListener scrollViewListener) {
        this.scrollViewListener = scrollViewListener;
    }

    @Override
    protected void onScrollChanged(int x, int y, int oldx, int oldy) {
        super.onScrollChanged(x, y, oldx, oldy);
        if (scrollViewListener != null) {
            scrollViewListener.onScrollChanged(this, x, y, oldx, oldy);
        }
    }
}

三、在布局中使用自定義控件

注意在 XML 中要寫對完整包名,否則會加載失敗:

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context=".MainActivity" >

    <com.example.demo1.ObservableScrollView
        android:id="@+id/view1"
        android:layout_width="wrap_content"
        android:layout_height="match_parent" >
        <!-- 內部放置若干 TextView 等子控件 -->
    </com.example.demo1.ObservableScrollView>

    <com.example.demo1.ObservableScrollView
        android:id="@+id/view2"
        android:layout_width="wrap_content"
        android:layout_height="match_parent" >
        <!-- 內部放置若干 TextView 等子控件 -->
    </com.example.demo1.ObservableScrollView>

</LinearLayout>

四、在 Activity 中使用

讓 Activity 實現 ScrollViewListener 接口,即可在滑動時做出響應,例如下面實現左右兩個列表的同步滑動:

public class MainActivity extends Activity implements ScrollViewListener {

    private ObservableScrollView scrollView1 = null;
    private ObservableScrollView scrollView2 = null;

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

        scrollView1 = (ObservableScrollView) findViewById(R.id.view1);
        scrollView1.setScrollViewListener(this);

        scrollView2 = (ObservableScrollView) findViewById(R.id.view2);
        scrollView2.setScrollViewListener(this);
    }

    @Override
    public void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy) {
        if (scrollView == scrollView1) {
            scrollView2.scrollTo(x, y);
        } else if (scrollView == scrollView2) {
            scrollView1.scrollTo(x, y);
        }
    }
}

通過這種方式,我們就能像使用 ListView 的 OnScrollListener 一樣,自由地在滑動過程中執行自定義邏輯。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved