Android 监听ScrollView的滑动
有时候我们需要监听ScroView的滑动情况,比如滑动了多少距离,是否滑到布局的顶部或者底部。可惜的是SDK并没有相应的方法,不过倒是提供了一个
- protected void onScrollChanged(int x, int y, int oldx, int oldy)
方法,显然这个方法是不能被外界调用的,因此就需要把它暴露出去,方便使用。解决方式就是写一个接口,
- package com.example.demo1;
- public interface ScrollViewListener {
- void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy);
- }
然后重写ScrollView类,给它提供上面写的回调接口。
- 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="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="horizontal"
- android:paddingBottom="@dimen/activity_vertical_margin"
- android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- tools:context=".MainActivity" >
- <com.example.demo1.ObservableScrollView
- android:id="@+id/view1"
- android:layout_width="wrap_content"
- android:layout_height="match_parent" >
- <LinearLayout
- android:layout_width="wrap_content"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <TextView
- android:layout_width="100dp"
- android:layout_height="100dp"
- android:text="试试1" />
- <TextView
- android:layout_width="100dp"
- android:layout_height="100dp"
- android:text="试试2" />
- <TextView
- android:layout_width="100dp"
- android:layout_height="100dp"
- android:text="试试3" />
- <TextView
- android:layout_width="100dp"
- android:layout_height="100dp"
- android:text="试试4" />
- <TextView
- android:layout_width="100dp"
- android:layout_height="100dp"
- android:text="试试5" />
- <TextView
- android:layout_width="100dp"
- android:layout_height="100dp"
- android:text="试试6" />
- </LinearLayout>
- </com.example.demo1.ObservableScrollView>
- <com.example.demo1.ObservableScrollView
- android:id="@+id/view2"
- android:layout_width="wrap_content"
- android:layout_height="match_parent" >
- <LinearLayout
- android:layout_width="wrap_content"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <TextView
- android:layout_width="100dp"
- android:layout_height="100dp"
- android:text="试试1" />
- <TextView
- android:layout_width="100dp"
- android:layout_height="100dp"
- android:text="试试2" />
- <TextView
- android:layout_width="100dp"
- android:layout_height="100dp"
- android:text="试试3" />
- <TextView
- android:layout_width="100dp"
- android:layout_height="100dp"
- android:text="试试4" />
- <TextView
- android:layout_width="100dp"
- android:layout_height="100dp"
- android:text="试试5" />
- <TextView
- android:layout_width="100dp"
- android:layout_height="100dp"
- android:text="试试6" />
- </LinearLayout>
- </com.example.demo1.ObservableScrollView>
- </LinearLayout>
最后activity代码如下,
- package com.example.demo1;
- import android.os.Bundle;
- import android.app.Activity;
- import android.view.Menu;
- 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 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;
- }
- @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);
- }
- }
- }
Android 监听ScrollView的滑动相关推荐
- Android监听ScrollView滑动到顶端和底部
package cn.testscrollview; import android.os.Bundle; import android.view.MotionEvent; import android ...
- Android监听屏幕的滑动事件
实现监听屏幕上下左右滑动事件 前段时间为了实现这个功能在网上找了很多教程,发现很多教程都是很大的篇幅,对于像我这种一点都不了解的人GestureListener的人来说可能很难理解,动不动就是几百行代 ...
- Android中监听ScrollView滑动停止和滑动到底部
1.监听ScrollView滑动停止: [java] view plaincopy /********************监听ScrollView滑动停止********************* ...
- android scrollview 滚动监听,Android开发之ScrollView的滑动监听
我们需要监听ScroView的滑动情况,比如滑动了多少距离,是否滑到布局的顶部或者底部.可惜的是SDK并没有相应的方法,不过倒是提供了一个 protected void onScrollChanged ...
- android 触摸监听重写_Android监听屏幕的滑动事件
实现监听屏幕上下左右滑动事件 前段时间为了实现这个功能在网上找了很多教程,发现很多教程都是很大的篇幅,对于像我这种一点都不了解的人GestureListener的人来说可能很难理解,动不动就是几百行代 ...
- Android 监听 WiFi 开关状态
Android 监听 WiFi 开关状态 转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/70854309 本文出自[赵彦军的博客] ...
- android 监听手机电量变化
今天,简单讲讲如何监听手机电量的变化. 监听电量是不能静态注册的. 后来上网搜索,发现有五个不能静态注册的广播,这里记录一下,免得下次再后知后觉的发现并惊讶于自己的笨拙. 不能静态注册的广播: and ...
- android 监听手机开机
今天,简单讲讲 android 监听手机开机. 需求描述:有些时候,我们需要我们的程序在开机后能自动运行,在系统即将关闭时,能写入一些记录到指定的文件里. 一.开机广播监听: Android系统 ...
- android 监听屏幕是否锁屏
今天,简单讲讲如何监听手机屏幕是否锁屏. 实现方法:1)通过BroadcastReceiver接收广播Intent.ACTION_SCREEN_ON和Intent.ACTION_SCREEN_OFF可 ...
最新文章
- 两条线段相切弧_两条直线间的圆弧连接
- C/C++中对链表操作的理解实例分析
- ABB 机器人 DRVIO_1通信报警
- jms mdb_MDB!= JMS,反之亦然
- Express学习笔记(基本使用,中间件,模块化路由,请求处理)
- Linux实战 | Centos6.8安装matlab的mount挂载问题的解决方法_3
- 13.TCP/IP 详解卷1 --- IGMP : Internet 管理组协议
- 数据结构与算法JavaScript描述——队列
- 图书馆管理系统——借书操作
- voc2012数据集详解
- Hadoop环境搭建与入门实例
- 【NLP_命名实体识别】Albert+BiLSTM+CRF模型训练、评估与使用
- 关于DSP2812的Timer0定时器配置程序的质疑
- 图解WinXP局域网共享设置步骤
- 决策树算法的Python实现—基于金融场景实操
- 中国浓咖啡行业市场供需与战略研究报告
- 网络系统管理赛项之Debian七. 2021年全国职业院校技能大赛将挑选各省1人或1组队伍参赛
- php json_encode不要反斜杠,【json+encode让URL内容斜杠+不转义】
- Vue入门之vue框架简介库和框架的区别MVVM介绍安装vuevue所管理的边界双向数据绑定vue事件vue的生命周期
- 11 Django REST Framework 针对基于类的视图添加 @csrf_exempt
热门文章
- 【Google Play】IARC 年龄分级 ( IARC 国际年龄分级联盟 | Google Play 设置应用年龄分级 )
- 【运筹学】匈牙利法 ( 克尼格定理 | 匈牙利法引入 )
- 【Kotlin】扩展属性 ( 扩展变量属性 | 扩展常量属性 | 注意事项 | 本质分析 )
- intellijidea课程 intellijidea神器使用技巧 3-1 列操作
- 重写equals就必须重写hashCode的原理分析
- POJ 3264 Balanced Lineup
- IbatisNet开发使用小结
- PHP创建XML文件讲解
- HTML5本地存储localstorage
- @Autowired