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

Trinea 发布于 2013/05/30 13:01
阅读 1K+
收藏 6
本文介绍 滚动到底部或顶部响应(如加载更多)的ScrollView的使用 。下面介绍使用自定义的ScrollView来完成该功能的实例。

本文可运行代码地址可见 BorderScrollViewDemo ,可运行APK地址 TrineaAndroidDemo.apk 。效果图如下:


BorderScrollView继承自ScrollView,可以自定义滚动到底部或顶部时需要完成的任务。使用如下:
一、自定义layout
只需将定义的ScrollView标签换成com.trinea.android.common.view.BorderScrollView标签即可,源码如下(其中的多个TextView只是为了将ScrollView撑满一屏幕):

layout xml源码
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
39
40
41
42
43
< com . trinea . android . common . view . BorderScrollView
     android : id = "@+id/scroll_view"
     android : layout_width = "match_parent"
     android : layout_height = "match_parent"
     android : layout_marginBottom = "@dimen/dp_40"
     android : orientation = "vertical" >
 
     < RelativeLayout
         android : layout_width = "match_parent"
         android : layout_height = "wrap_content" >
 
         < TextView
             android : id = "@+id/top_text"
             android : layout_width = "match_parent"
             android : layout_height = "20dp"
             android : gravity = "center"
             android : text = "top text" / >
 
         < TextView
             android : id = "@+id/text1"
             android : layout_width = "match_parent"
             android : layout_height = "wrap_content"
             android : layout_below = "@+id/top_text"
             android : gravity = "center"
             android : text = "text1" / >
 
         < TextView
             android : id = "@+id/text2"
             android : layout_width = "match_parent"
             android : layout_height = "wrap_content"
             android : layout_below = "@+id/text1"
             android : gravity = "center"
             android : text = "text2" / >
 
         < TextView
             android : id = "@+id/bottom_text"
             android : layout_width = "match_parent"
             android : layout_height = "20dp"
             android : layout_below = "@+id/text2"
             android : gravity = "center"
             android : text = "bottom text" / >
     < / RelativeLayout >
< / com . trinea . android . common . view . BorderScrollView >

 

2、设置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 ( ) {
 
             @Override
             public void onTop ( ) {
                 // may be done multi times, u should control it
                 Toast . makeText ( context , "has reached top" , Toast . LENGTH_SHORT ) . show ( ) ;
             }
 
             @Override
             public void onBottom ( ) {
                 // may be done multi times, u should control it
                 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 ) ;
     }
}

注意onTop和onBottom是有可能被多次执行的,需要自己控制,在后面的实现原理中介绍具体原因。

 

你可能还感兴趣:

scrollview中viewpager无法正常滑动问题

添加ScrollView后或外部容器为RelativeLayout时onFling不起作用,无法滑动问题

Android系统下载管理DownloadManager功能介绍及使用示例

Android性能优化经验总结


以下是话题补充:

@Trinea:Demo下载地址变更:https://play.google.com/store/apps/details?id=cn.trinea.android.demo (2014/01/05 12:21)
返回顶部
顶部