1

I have created a custom scroll view and added some images views dynamically to it. All I wanted to do is to scroll the scrollview to a particular position when the view is initialized. I used scrollview.smoothScrollTo() method to move the scrollview to particular position. It is working perfectly in Android 2.1 but when in android 2.2 it is not working. The scrollview always goes to the initial position. Whwn I used the same method in OnClick Listener of a button it is working. Please help.

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.galleryitem_details);
    hscrollView = (HorizontalScrollView)findViewById(R.id.photolist_scrollview);
            addImages();
            hscrollView.smoothScrollTo(1000, 0);

}


           private void addImages() {


    for(int i = 0; i< 10; i++) {

        View imageDetailView = View.inflate(this, R.layout.gallery_image, null);
        ImageView image = (ImageView)imageDetailView.findViewById(R.id.gallery_imagebig);
        TextView txtView = (TextView)imageDetailView.findViewById(R.id.imageBigText);
        ProgressBar spinner = (ProgressBar)imageDetailView.findViewById(R.id.gallery_scrollimage);
        txtView.setWidth(screenWidth);
             imageDetailView.setBackGroundResource(R.drawable.icon);
        txtView.setText(PHGalleryActivity.galleryImages.get(i).getImageCaption());
        imageDetailLayout.addView(imageDetailView);
    }
    /*
     *  Set the position of scroll view.
     */


}

the hscrollview is not scrolled to the position. The scrollview starts from initial postion.

2 Answers 2

2

The issue is solved. I don't know what exactly the issue. I used Handler for setting the offset value of the scrollview. Issue is occurs when we try to set offset of scrollview in onCreate(). In other cases suppose if we set offset value inside onClick() this works perfectly without using handler.

 protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   requestWindowFeature(Window.FEATURE_NO_TITLE);
   setContentView(R.layout.galleryitem_details);
   hscrollView = (HorizontalScrollView)findViewById(R.id.photolist_scrollview);
   addImages();
   updateUI();
}


    class MHandler extends Handler {

    public void handleMessage(Message msg) {
        PHGalleryItemActivity.this.updateUI();
    }

    public void sleep(long delayMillis) {
      this.removeMessages(0);
      sendMessageDelayed(obtainMessage(0), delayMillis);
    }
  };

  private void updateUI(){
      mHandler.sleep(0);
     hscrollView.smoothScrollTo(1000, 0);

  }
1

I had the same problem. I managed to solve it by overriding the onDraw() method of a subclass of HorizontalScrollview. After the super.onDraw() returns I call a method on the MainActivity. This method, the first time it is called, does a smoothScrollTo(x,0) and it worked fine as a replacement of smoothScrollTo() in the onCreate method.

In a subclassed HorizontalScrollView:

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    MainActivity act = (MainActivity) this.getContext();
    act.horizontalScrollViewDrawn();

}

In the MainActivity:

private boolean firstRun = true;
private hsv= null;

public onCreate(...){
    ..
   hsv = (HorizontalScrollView) findViewById(R.id.hsv);
    ..
}

public void horizontalScrollViewDrawn() {
    if(firstRun){
            hsv.smoothScrollTo(X, 0);
            firstRun = false;
    }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.