
时候我们需要监听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 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 oldy) {
super.onScrollChanged(x,y,oldx,oldy);
if (scrollVIEwListener != null) {
scrollVIEwListener.onScrollChanged(this,x,oldy);
}
}
}
注意在xml布局的时候,不要写错了包。
复制代码 代码如下:@H_403_79@
<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 oldx,int oldy) {
if (scrollVIEw == scrollVIEw1) {
scrollVIEw2.scrollTo(x,y);
} else if (scrollVIEw == scrollVIEw2) {
scrollVIEw1.scrollTo(x,y);
}
}
}
总结
以上是内存溢出为你收集整理的Android中实现监听ScrollView滑动事件全部内容,希望文章能够帮你解决Android中实现监听ScrollView滑动事件所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)