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

建议直接访问原文:滚动到底部或顶部响应的ScrollView使用

本文介绍滚动到底部或顶部响应(如加载更多)的ScrollView的使用。
关于实现原理可见:滑动到底部或顶部响应的ScrollView实现

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


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

<cn.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()两个函数可以实现,分别在滑动到顶部和底部时被调用执行。代码如下:

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性能优化经验总结滚动到底部或顶部响应的ScrollView使用

 

标题Python网络课程在线学习平台研究AI更换标题第1章引言介绍Python网络课程在线学习平台的研究背景、意义、国内外现状和研究方法。1.1研究背景与意义阐述Python在线学习平台的重要性和研究意义。1.2国内外研究现状概述国内外Python在线学习平台的发展现状。1.3研究方法与论文结构介绍本文的研究方法和整体论文结构。第2章相关理论总结在线学习平台及Python教育的相关理论。2.1在线学习平台概述介绍在线学习平台的基本概念、特点和发展趋势。2.2Python教育理论阐述Python语言教学的理论和方法。2.3技术支持理论讨论构建在线学习平台所需的技术支持理论。第3章Python网络课程在线学习平台设计详细介绍Python网络课程在线学习平台的设计方案。3.1平台功能设计阐述平台的核心功能,如课程管理、用户管理、学习跟踪等。3.2平台架构设计给出平台的整体架构,包括前后端设计、数据库设计等。3.3平台界面设计介绍平台的用户界面设计,强调用户体验和易用性。第4章平台实现与测试详细阐述Python网络课程在线学习平台的实现过程和测试方法。4.1平台实现介绍平台的开发环境、技术栈和实现细节。4.2平台测试对平台进行功能测试、性能测试和安全测试,确保平台稳定可靠。第5章平台应用与效果分析分析Python网络课程在线学习平台在实际应用中的效果。5.1平台应用案例介绍平台在实际教学培训中的应用案例。5.2效果评估与分析通过数据分析和用户反馈,评估平台的应用效果。第6章结论与展望总结Python网络课程在线学习平台的研究成果,并展望未来发展方向。6.1研究结论概括本文关于Python在线学习平台的研究结论。6.2研究展望提出未来Python在线学习平台的研究方向和发展建议。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值