ScrollView滚动监听事件

package com.qf.textscor;


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


public class MainActivity extends Activity {

private ScrollView mScrollView;


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


private void init(){
       mScrollView=(ScrollView) findViewById(R.id.scrollView);
       mScrollView.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:


/**
  * Demo描述:
  * 监听ScrollView滑动到顶端和底部
  *
  * 注意事项:
  * 1 mScrollView.getChildAt(0).getMeasuredHeight()表示:
  *   ScrollView所占的高度.即ScrollView内容的高度.常常有一
  *   部分内容要滑动后才可见,这部分的高度也包含在了
  *   mScrollView.getChildAt(0).getMeasuredHeight()中
  *  
  * 2 view.getScrollY表示:
  *   ScrollView顶端已经滑出去的高度
  *  
  * 3 view.getHeight()表示:
  *   ScrollView的可见高度
  *  
  */

                int scrollY=view.getScrollY();
                int height=view.getHeight();
                int scrollViewMeasuredHeight=mScrollView.getChildAt(0).getMeasuredHeight();
                if(scrollY==0){
                      // System.out.println("滑动到了顶端 view.getScrollY()="+scrollY);
                Log.e("滑动到了顶端 view.getScrollY()=", scrollY+"");
                   }
                if((scrollY+height)==scrollViewMeasuredHeight){
                       System.out.println("滑动到了底部 scrollY="+scrollY);
                       Log.e("滑动到了底部 scrollY=", scrollY+"");
                       System.out.println("滑动到了底部 height="+height);
                       Log.e("滑动到了底部 height=", height+"");
                       System.out.println("滑动到了底部 scrollViewMeasuredHeight="+scrollViewMeasuredHeight);
                       Log.e("滑动到了底部 scrollViewMeasuredHeight=", scrollViewMeasuredHeight+"");
                   }
               break;
 
           default:
               break;
           }
           return false;
       }
        
   };

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}


}




//布局:

<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"
    >
    <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
        <LinearLayout 
            android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
            >
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"/>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"/>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"/>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"/>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"/>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"/>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"/>
    
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"/>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"/>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"/>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"/>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"/>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"/>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"/>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"/>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"/>
    
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"/>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"/>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"/>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"/>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"/>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"/>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"/>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"/>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"/>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"/>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"/>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"/>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"/>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"/>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"/>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"/>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"/>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"/>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"/>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"/>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"/>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"/>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"/>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"/>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"/>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"/>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"/>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"/>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"/>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"/>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"/>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"/>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"/>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"/>
    
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"/>
    
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"/>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"/>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"/>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"/>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"/>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"/>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"/>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"/>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"/>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"/>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"/>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"/>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"/>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"/>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"/>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"/>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"/>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"/>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"/>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"/>
     </LinearLayout>
    </ScrollView>
    
       
</RelativeLayout>










完美解决手离开后滚动的问题:

sv_home_main.setOnTouchListener(new OnTouchListener() {
  private int lastY = 0;
           private int touchEventId = -9983761;
           Handler handler = new Handler() {
               @Override
               public void handleMessage(Message msg) {
                   super.handleMessage(msg);
                   View scroller = (View) msg.obj;
                    
                   if (msg.what == touchEventId) {
                       if (lastY == scroller.getScrollY()) {
                       } else {
                           handler.sendMessageDelayed(handler.obtainMessage(touchEventId, scroller), 1);
                           lastY = scroller.getScrollY();
                       }
                       
                       if (lastY<180) {
                        home_top.setBackgroundColor(getActivity().getResources().getColor(R.color.transparent));
                       
} else {
home_top.setBackgroundColor(getActivity().getResources().getColor(R.color.btn_text));
}
                       
                       
                   }
                   
                   
               }
           };
 
 
           @Override
           public boolean onTouch(View v, MotionEvent event) {
              // isScoll = false;
               int eventAction = event.getAction();
               int y = (int) event.getRawY();
               switch (eventAction) {
               case MotionEvent.ACTION_UP:
                    
                   handler.sendMessageDelayed(handler.obtainMessage(touchEventId, v), 30);
                
                   break;
                
               default:
                   break;
               }
               return false;
           }
});

### 回答1: Android中的ScrollView滚动监听可以通过设置OnScrollChangeListener来实现。具体步骤如下: 1. 在布局文件中添加ScrollView控件,并设置其id。 2. 在Java代码中获取ScrollView控件,并设置OnScrollChangeListener。 3. 在OnScrollChangeListener中实现滚动监听的逻辑。 示例代码如下: XML布局文件: <ScrollView android:id="@+id/scrollView" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- 添加需要滚动的内容 --> </ScrollView> Java代码: ScrollView scrollView = findViewById(R.id.scrollView); scrollView.setOnScrollChangeListener(new View.OnScrollChangeListener() { @Override public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) { // 在此处实现滚动监听的逻辑 } }); 在onScrollChange方法中,可以获取ScrollView滚动位置scrollX和scrollY,以及上一次的滚动位置oldScrollX和oldScrollY。通过这些参数,可以实现各种滚动监听的逻辑,例如:滚动到底部加载更多数据、滚动到顶部显示“回到顶部”按钮等。 ### 回答2: Android的ScrollView控件是一个可以放置多个子控件的视图容器,用于实现可滚动的UI界面。在Android中,我们可以通过设置ScrollView滚动监听监听ScrollView控件的滚动状态,从而对UI界面进行动态的调整和处理。 ScrollView控件的滚动监听主要包括两个方面:滚动状态的监听滚动位置的监听滚动状态的监听可以通过设置ScrollView的setOnScrollChangeListener()方法来实现。该方法会在ScrollView滚动状态改变时被调用,并传递当前ScrollView滚动状态、滚动距离以及滚动速度等相关参数。我们可以根据这些参数来动态地调整UI界面的布局和显示效果。 例如,我们可以利用滚动状态的监听来实现下拉刷新的效果:当用户下拉ScrollView时,我们可以通过监听ScrollView滚动状态来判断用户已经下拉到了一定的距离,并在这个状态下展示一个带有下拉箭头的刷新提示框。当用户松开手指,ScrollView返回到原来的位置时,我们可以通过滚动状态的监听来判断滚动是否已经停止,并执行数据更新的操作。 滚动位置的监听可以通过重写ScrollView的onScrollChanged()方法来实现。该方法会在ScrollView滚动位置发生改变时被调用,并传递当前ScrollView相对于顶部的偏移量。我们可以根据这个偏移量来实现一些动态的效果,比如浮动的导航栏、滚动到一定位置后自动展开的菜单等。 总之,ScrollView滚动监听在Android中是非常常用的功能,尤其是对于需要实现可滚动界面的应用程序。通过设置滚动监听,我们可以实现一些非常丰富和动态的UI效果,让应用程序的用户体验更加流畅和便捷。 ### 回答3: Android ScrollView是一个常用的控件,用于在屏幕上展示超过屏幕高度的内容。随着用户向下或向上滑动,这些内容会随之滚动。 在某些情况下,我们希望在用户滚动过程中对ScrollView进行一些操作。这时,就需要使用ScrollView滚动监听功能。 ScrollView滚动监听可以通过setOnScrollChangeListener()方法来实现。该方法需要传入一个OnScrollChangeListener接口,该接口中包含一个onScrollChange()方法,可以在ScrollView滚动时被调用。下面是一个简单的示例: scrollView.setOnScrollChangeListener(new View.OnScrollChangeListener() { @Override public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) { // 在此处添加需要执行的操作,例如: if(scrollY > oldScrollY) { Log.d("ScrollView", "向下滚动"); } else { Log.d("ScrollView", "向上滚动"); } } }); 在onScrollChange()方法中,我们可以获取ScrollView当前的滚动位置(即scrollX和scrollY)。如果要执行某些需要在滚动过程中不断改变的操作,例如某个View的透明度或位置,我们可以在此处进行计算并实时更新View的状态。 除此之外,我们还可以通过ScrollView的getScrollX()和getScrollY()方法来获取ScrollView当前的滚动位置。此外,我们可以从onScrollChange()方法中获取被滚动View对象(即ScrollView本身)。这些方法都可以在我们需要在滚动过程中对ScrollView进行控制时有很大帮助。 总体而言,ScrollView滚动监听功能为我们在ScrollView滚动过程中添加各种操作提供了便利。结合其他Android UI控件,我们可以创建出更灵活、更多样化且物美价廉的用户界面。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值