浏览 3439 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2013-05-31
关于实现原理可见:滑动到底部或顶部响应的ScrollView实现 本文可运行代码地址可见BorderScrollViewDemo,可运行APK地址TrineaAndroidDemo.apk。效果图如下: ![]() BorderScrollView继承自ScrollView,可以自定义滚动到底部或顶部时需要完成的任务。使用如下: 一、自定义layout 只需将定义的ScrollView标签换成com.trinea.android.common.view.BorderScrollView标签即可,源码如下(其中的多个TextView只是为了将ScrollView撑满一屏幕): <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()两个函数可以实现,分别在滑动到顶部和底部时被调用执行。代码如下: 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使用 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |