Android 监听ScrollView的滑动

    有时候我们需要监听ScroView的滑动情况,比如滑动了多少距离,是否滑到布局的顶部或者底部。可惜的是SDK并没有相应的方法,不过倒是提供了一个 

  1. protected void onScrollChanged(int x, int y, int oldx, int oldy)  

   方法,显然这个方法是不能被外界调用的,因此就需要把它暴露出去,方便使用。解决方式就是写一个接口, 

  1. package com.example.demo1;  
  2.   
  3. public interface ScrollViewListener {  
  4.   
  5.     void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy);  
  6.   
  7. }  

 

 

  然后重写ScrollView类,给它提供上面写的回调接口。 

  1. package com.example.demo1;  
  2.   
  3. import android.content.Context;  
  4. import android.util.AttributeSet;  
  5. import android.widget.ScrollView;  
  6.   
  7. public class ObservableScrollView extends ScrollView {  
  8.   
  9.     private ScrollViewListener scrollViewListener = null;  
  10.   
  11.     public ObservableScrollView(Context context) {  
  12.         super(context);  
  13.     }  
  14.   
  15.     public ObservableScrollView(Context context, AttributeSet attrs,  
  16.             int defStyle) {  
  17.         super(context, attrs, defStyle);  
  18.     }  
  19.   
  20.     public ObservableScrollView(Context context, AttributeSet attrs) {  
  21.         super(context, attrs);  
  22.     }  
  23.   
  24.     public void setScrollViewListener(ScrollViewListener scrollViewListener) {  
  25.         this.scrollViewListener = scrollViewListener;  
  26.     }  
  27.   
  28.     @Override  
  29.     protected void onScrollChanged(int x, int y, int oldx, int oldy) {  
  30.         super.onScrollChanged(x, y, oldx, oldy);  
  31.         if (scrollViewListener != null) {  
  32.             scrollViewListener.onScrollChanged(this, x, y, oldx, oldy);  
  33.         }  
  34.     }  
  35.   
  36. }  



 

  注意在xml布局的时候,不要写错了包。 

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="horizontal"  
  6.     android:paddingBottom="@dimen/activity_vertical_margin"  
  7.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  8.     android:paddingRight="@dimen/activity_horizontal_margin"  
  9.     android:paddingTop="@dimen/activity_vertical_margin"  
  10.     tools:context=".MainActivity" >  
  11.   
  12.     <com.example.demo1.ObservableScrollView  
  13.         android:id="@+id/view1"  
  14.         android:layout_width="wrap_content"  
  15.         android:layout_height="match_parent" >  
  16.   
  17.         <LinearLayout  
  18.             android:layout_width="wrap_content"  
  19.             android:layout_height="match_parent"  
  20.             android:orientation="vertical" >  
  21.   
  22.             <TextView  
  23.                 android:layout_width="100dp"  
  24.                 android:layout_height="100dp"  
  25.                 android:text="试试1" />  
  26.   
  27.             <TextView  
  28.                 android:layout_width="100dp"  
  29.                 android:layout_height="100dp"  
  30.                 android:text="试试2" />  
  31.   
  32.             <TextView  
  33.                 android:layout_width="100dp"  
  34.                 android:layout_height="100dp"  
  35.                 android:text="试试3" />  
  36.   
  37.             <TextView  
  38.                 android:layout_width="100dp"  
  39.                 android:layout_height="100dp"  
  40.                 android:text="试试4" />  
  41.   
  42.             <TextView  
  43.                 android:layout_width="100dp"  
  44.                 android:layout_height="100dp"  
  45.                 android:text="试试5" />  
  46.   
  47.             <TextView  
  48.                 android:layout_width="100dp"  
  49.                 android:layout_height="100dp"  
  50.                 android:text="试试6" />  
  51.         </LinearLayout>  
  52.     </com.example.demo1.ObservableScrollView>  
  53.   
  54.     <com.example.demo1.ObservableScrollView  
  55.         android:id="@+id/view2"  
  56.         android:layout_width="wrap_content"  
  57.         android:layout_height="match_parent" >  
  58.   
  59.         <LinearLayout  
  60.             android:layout_width="wrap_content"  
  61.             android:layout_height="match_parent"  
  62.             android:orientation="vertical" >  
  63.   
  64.             <TextView  
  65.                 android:layout_width="100dp"  
  66.                 android:layout_height="100dp"  
  67.                 android:text="试试1" />  
  68.   
  69.             <TextView  
  70.                 android:layout_width="100dp"  
  71.                 android:layout_height="100dp"  
  72.                 android:text="试试2" />  
  73.   
  74.             <TextView  
  75.                 android:layout_width="100dp"  
  76.                 android:layout_height="100dp"  
  77.                 android:text="试试3" />  
  78.   
  79.             <TextView  
  80.                 android:layout_width="100dp"  
  81.                 android:layout_height="100dp"  
  82.                 android:text="试试4" />  
  83.   
  84.             <TextView  
  85.                 android:layout_width="100dp"  
  86.                 android:layout_height="100dp"  
  87.                 android:text="试试5" />  
  88.   
  89.             <TextView  
  90.                 android:layout_width="100dp"  
  91.                 android:layout_height="100dp"  
  92.                 android:text="试试6" />  
  93.         </LinearLayout>  
  94.     </com.example.demo1.ObservableScrollView>  
  95.   
  96. </LinearLayout>  

 

  最后activity代码如下, 

  1. package com.example.demo1;  
  2.   
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.view.Menu;  
  6.   
  7. public class MainActivity extends Activity implements ScrollViewListener {  
  8.   
  9.     private ObservableScrollView scrollView1 = null;  
  10.     private ObservableScrollView scrollView2 = null;  
  11.   
  12.     @Override  
  13.     protected void onCreate(Bundle savedInstanceState) {  
  14.         super.onCreate(savedInstanceState);  
  15.         setContentView(R.layout.activity_main);  
  16.   
  17.         scrollView1 = (ObservableScrollView) findViewById(R.id.view1);  
  18.         scrollView1.setScrollViewListener(this);  
  19.         scrollView2 = (ObservableScrollView) findViewById(R.id.view2);  
  20.         scrollView2.setScrollViewListener(this);  
  21.   
  22.     }  
  23.   
  24.     @Override  
  25.     public boolean onCreateOptionsMenu(Menu menu) {  
  26.         // Inflate the menu; this adds items to the action bar if it is present.  
  27.         getMenuInflater().inflate(R.menu.main, menu);  
  28.         return true;  
  29.     }  
  30.   
  31.     @Override  
  32.     public void onScrollChanged(ObservableScrollView scrollView, int x, int y,  
  33.             int oldx, int oldy) {  
  34.         if (scrollView == scrollView1) {  
  35.             scrollView2.scrollTo(x, y);  
  36.         } else if (scrollView == scrollView2) {  
  37.             scrollView1.scrollTo(x, y);  
  38.         }  
  39.     }  
  40.   
  41. }  
  

 

 

  

posted @ 2014-02-27 21:29  安卓吧  阅读(82727)  评论(6编辑  收藏  举报