安卓ScrollView实现自动滚屏

  今天因项目需要,制作一个ScrollView的自动滚屏。在网上找了很多的代码,还是无法具体实现。具体网页有这些:比如http://www.eoeandroid.com/forum.php?mod=viewthread&tid=46764,都是大同小异。主要的实现代码就是这些。可是仍然会有人不知道具体怎么使用。比如,布局xml你可能写的不符合规则。还有可能不知道放在哪里才可以实现滚屏。

  首先,先把他的代码贴出来:

首先1

获得ScrollView sc = (ScrollView) findViewById(R.id.scroll);//scroll对象
     LinearLayout mlayout = (LinearLayout) findViewById(R.id.mlayout);//scrollView中包含的布局对象
2,定义一个Handler
private final Handler mHandler = new Handler();

<span style="font-family:Arial;">private Runnable ScrollRunnable= new Runnable() {
                @Override
                public void run() {
                                     int off = mlayout.getMeasuredHeight() - sc.getHeight();//判断高度 </span>
<span style="white-space: pre;">		</span><span style="font-family:Arial;">if (off > 0) {</span>
<span style="font-family:Arial;">                                sc.scrollBy(0, 30);
                                if (sc.getScrollY() == off) {
                                        Thread.currentThread().interrupt();
                                } else {
                                        mHandler.postDelayed(this, 1000);
                                }
                        }
                }
        };</span>
这就是核心代码,当然还有一部分代码,一般搜出来的网页上是没有的。那就是怎么具体用他,还需要设置一个监听器,就是addOnLayoutChangeListener。在这个监听器里面调用 mHandler.post(ScrollRunnable),就可以了。

样板布局如下:

<span style="font-family:Arial;"><ScrollView
        xmlns:android="http://schemas.android.com/apk/res/android"
	    xmlns:tools="http://schemas.android.com/tools"
	    android:layout_width="match_parent"
	    android:layout_height="match_parent"
	    android:id="@+id/nameScroll" >
        
        <LinearLayout
            android:layout_width="match_parent"
   		 	android:layout_height="100dp"
		    android:orientation="vertical"
		    	    android:focusable="false"  
            android:focusableInTouchMode="false"
		    android:id="@+id/layout" >
            
        </LinearLayout>
</ScrollView></span>
 
具体实现就不用赘述了。留下一个Demo供大家参考~

下载地址:http://pan.baidu.com/s/1dD6FPeD 





  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值