天天看點

滾動到底部或頂部響應的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);