ScrollView当显示超出当前页面时自动移动到最底端

这篇博客介绍了如何在Android中实现ScrollView在内容超出当前视口时自动滚动到底部的方法。通过Handler和Runnable,结合ScrollView和LinearLayout的高度差来实现自动滚动。同时,文中也展示了多种不同的滚动到底部的实现方式,包括直接使用ScrollView的fullScroll方法和自定义的scrollToBottom方法。
摘要由CSDN通过智能技术生成
卷轴视图(ScrollView)是指当拥有很多内容,一屏显示不完时,需要通过滚动来显示视图。比如在做一个阅读器的时候,文章很长,一页显示不完,那么就需要使用卷轴视图来滚动显示下一页。
Java代码 复制代码  收藏代码
  1. private ScrollView mScrollView;   
  2. private LinearLayout mLayout;   
  3. private final Handler mHandler = new Handler();   
  4.   
  5. mScrollView = (ScrollView)findViewById(R.id.scroll);   
  6. mLayout = (LinearLayout)findViewById(R.id.linearlayout);//linearlayout外层为 scroll   
  7. mHandler.post(mScrollToBottom);   
  8.   
  9. private Runnable mScrollToBottom = new Runnable() {       
  10.      @Override  
  11.      public void run() {   
  12.       // TODO Auto-generated method stub   
  13.       int off = mLayout.getMeasuredHeight() - mScrollView.getHeight();   
  14.       if (off > 0) {   
  15.        mScrollView.scrollTo(0, off);   
  16.        }   
  17.       }   
  18.      };  
private ScrollView mScrollView;
private LinearLayout mLayout;
private final Handler mHandler = new Handler();

mScrollView = (ScrollView)findViewById(R.id.scroll);
mLayout = (LinearLayout)findViewById(R.id.linearlayout);//linearlayout外层为 scroll
mHandler.post(mScrollToBottom);

private Runnable mScrollToBottom = new Runnable() {    
     @Override
     public void run() {
      // TODO Auto-generated method stub
      int off = mLayout.getMeasuredHeight() - mScrollView.getHeight();
      if (off > 0) {
       mScrollView.scrollTo(0, off);
       }
      }
     };

在Android,一个单独的TextView是无法滚动的,需要放在一个ScrollView中。ScrollView提供了一系列的函数,其中fullScroll用来实现home和end键的功能,也就是滚动到顶部和底部。

但是,如果在TextView的append后面马上调用fullScroll,会发现无法滚动到真正的底部,这是因为Android下很多(如果不是全部的话)函数都是基于消息的,用消息队列来保证同步,所以函数调用多数是异步操作的。当TextView调用了append会,并不等text显示出来,而是把text的添加到消息队列之后立刻返回,fullScroll被调用的时候,text可能还没有显示,自然无法滚动到正确的位置。

解决的方法其实也很简单,使用post:
Java代码 复制代码  收藏代码
  1. final ScrollView svResult = (ScrollView) findViewById(R.id.svResult);   
  2. svResult.post(new Runnable() {   
  3.         public void run() {   
  4.             svResult.fullScroll(ScrollView.FOCUS_DOWN);   
  5.         }   
  6. });  
final ScrollView svResult = (ScrollView) findViewById(R.id.svResult);
svResult.post(new Runnable() {
    	public void run() {
    	    svResult.fullScroll(ScrollView.FOCUS_DOWN);
    	}
});



Android将ScrollView移动到最底部
scrollTo方法可以调整view的显示位置。
在需要的地方调用以下方法即可。
scroll表示外层的view,inner表示内层的view,其余内容都在inner里。
注意,方法中开一个新线程是必要的。
否则在数据更新导致换行时getMeasuredHeight方法并不是最新的高度。

Java代码 复制代码  收藏代码
  1. public static void scrollToBottom(final View scroll, final View inner) {   
  2.   
  3. Handler mHandler = new Handler();   
  4.     
  5. mHandler.post(new Runnable() {   
  6. public void run() {   
  7. if (scroll == null || inner == null) {   
  8. return;   
  9. }   
  10.   
  11. int offset = inner.getMeasuredHeight() - scroll.getHeight();   
  12. if (offset < 0) {   
  13. offset = 0;   
  14. }   
  15.   
  16. scroll.scrollTo(0, offset);   
  17. }   
  18. });   
  19. }  
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值