HorizontalScrollView 初始化滚动的实现

要实现Horizontal初始化水平滚动,如果直接调用方法smoothScrollTo(int x,int y),达不到效果。这里需要借助Handler.

A Handler allows you to send and process Message and Runnable objects associated with a thread's MessageQueue. Each Handler instance is associated with a single thread and that thread's message queue. When you create a new Handler, it is bound to the thread / message queue of the thread that is creating it -- from that point on, it will deliver messages and runnables to that message queue and execute them as they come out of the message queue.

There are two main uses for a Handler: (1) to schedule messages and runnables to be executed as some point in the future; and (2) to enqueue an action to be performed on a different thread than your own.

Scheduling messages is accomplished with the post(Runnable), postAtTime(Runnable, long), postDelayed(Runnable, long), sendEmptyMessage(int), sendMessage(Message), sendMessageAtTime(Message, long), and sendMessageDelayed(Message, long) methods. The post versions allow you to enqueue Runnable objects to be called by the message queue when they are received; the sendMessage versions allow you to enqueue a Message object containing a bundle of data that will be processed by the Handler's handleMessage(Message) method (requiring that you implement a subclass of Handler).

When posting or sending to a Handler, you can either allow the item to be processed as soon as the message queue is ready to do so, or specify a delay before it gets processed or absolute time for it to be processed. The latter two allow you to implement timeouts, ticks, and other timing-based behavior.

When a process is created for your application, its main thread is dedicated to running a message queue that takes care of managing the top-level application objects (activities, broadcast receivers, etc) and any windows they create. You can create your own threads, and communicate back with the main application thread through a Handler. This is done by calling the same post or sendMessage methods as before, but from your new thread. The given Runnable or Message will then be scheduled in the Handler's message queue and processed when appropriate. 


通过多次阅读,可以知道,handler有两种用法:1.发送消息到创建该Handler的线程的消息队列里,并通过handlerMessage处理该消息 2.发送任务Runnable,给创建该Handler的线程执行。

第一种用法,涉及到的发送方法有:sendEmptyMessage(int), sendMessage(Message), sendMessageAtTime(Message, long), and sendMessageDelayed(Message, long)

第二种用法,涉及到的发送方法有:post(Runnable), postAtTime(Runnable, long), postDelayed(Runnable, long)

实现初始化时滚动,这里用到Handler的第二种用法,调用postDelayed(Runnable, long)即可。

附上代码:

@Override
protected void onResume() {
    super.onResume();
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            // mCurrentIndex表示要滚动的位置序号,5为一屏显示的国家图片数量,mCountryContainerWidth代表一个国家所占用的屏幕宽度(单位px)
            mScrollView.smoothScrollTo(mCountryContainerWidth * 5 * (mCurrentIndex / 5), 0);
        }
    }, 800);

}

在这里,需要留意postDelayed(Runnable, long)的第二参数,该参数数值不可过小。我在运行该方法时,如果设置成2,滚动效果就出不来。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值