Search code examples
androidhorizontalscrollview

Auto slow scroll with HorizontalScrollView


I have HorizontalScrollView and 6 ImageViews in it. I'm getting images from Parse.com. I want the HorizontalScrollView to scroll slowly when the activity starts. I have gone through the references but couldn't resolve it. I tried this method :

 new Handler().postDelayed(new Runnable() {
        public void run() {
            horizontalScrollView1.fullScroll(HorizontalScrollView.FOCUS_RIGHT);
            //horizontalScrollView1.smoothScrollBy(500, 0);
        }
    }, 4000L);

But with this, it is scrolling too fast. Maybe I could process HorizontalScrollView in a loop, so that each ImageView goes on repeating itself infinitely. Is there any way to do it. Thanks


Solution

  • Check this one..

    android.widget.HorizontalScrollView horScrlView;
    
    Handler mHandler = new Handler(new Callback() {
    
        @Override
        public boolean handleMessage(Message msg) {
            horScrlView.smoothScrollBy(5, 0);
            mHandler.sendMessageDelayed(new Message(), 1000);
            return false;
        }
    });
    
    /* (non-Javadoc)
     * @see android.app.Activity#onCreate(android.os.Bundle)
     * @author Hardik Shah
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.horizontal_scroll_view);
        horScrlView = (android.widget.HorizontalScrollView) findViewById(R.id.horizontalScrollView1);
    
        mHandler.sendMessageDelayed(new Message(), 1000);
    }