天天看点

滚动到底部或顶部响应的ScrollView使用

listview示例很多,对于scrollview却寥寥无几,下面介绍使用自定义的scrollview来完成该功能的实例。

滚动到底部或顶部响应的ScrollView使用

1、引入公共库

2、自定义layout

只需将定义的scrollview标签换成cn.trinea.android.common.view.borderscrollview标签即可,源码如下(其中的多个textview只是为了将scrollview撑满一屏幕):

layout xml源码

3、设置ontop和onbottom事件

通过borderscrollview.setonborderlistener(onborderlistener onborderlistener)设置到达底部和顶部的响应。

onborderlistener有ontop()和void onbottom()两个函数可以实现,分别在滑动到顶部和底部时被调用执行。代码如下:

java部分实现源码

java

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

public class borderscrollviewdemo extends activity {

private borderscrollview borderscrollview;

private textview textview1;

private textview textview2;

private context context;

@override

protected void oncreate(bundle savedinstancestate) {

super.oncreate(savedinstancestate);

setcontentview(r.layout.border_scroll_view_demo);

context = getapplicationcontext();

borderscrollview = (borderscrollview)findviewbyid(r.id.scroll_view);

borderscrollview.setonborderlistener(new onborderlistener() {

public void ontop() {

// may be done multi times, u should control it

toast.maketext(context, "has reached top", toast.length_short).show();

}

public void onbottom() {

toast.maketext(context, "has reached bottom", toast.length_short).show();

});

textview1 = (textview)findviewbyid(r.id.text1);

textview2 = (textview)findviewbyid(r.id.text2);

display display = getwindowmanager().getdefaultdisplay();

textview1.setheight(display.getheight() / 2);

textview2.setheight(display.getheight() / 2);