Android ViewFlipper animating only the first URL image

1.4k views Asked by At

I have an android activity with a ViewFlipper. It picks images from a url and displays in a layout. It is supposed to animate though all the images. However, it only animates the first url image and it keeps repeating. It does not display the other images. Below is the code:

public class DynamicViewFlipper extends Activity {

/**
 * List of Image URL that will populate the ViewFlipper
 */
private List<String> imageURLs = Arrays.asList(new String[] { 
        "http://example.com/image1.jpg",
        "http://example.com/image2.jpg", 
        "http://example.com/image3.jpg",
        "http://example.com/image4.jpg"}); 



private int index = 0;

private TextView mTextView;

private ViewFlipper mViewFlipper;

private Button mNextButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_view_flipper);
    mTextView = (TextView) findViewById(R.id.title);
    mViewFlipper = (ViewFlipper) findViewById(R.id.viewFlipper);


            ImageView image = new ImageView(getApplicationContext());
            UrlImageViewHelper.setUrlDrawable(image, getNextImage(), R.drawable.loading);
            mTextView.setText("Showing: " + index);
            mViewFlipper.addView(image);
            mViewFlipper.showNext();
            mViewFlipper.setAutoStart(true);
            mViewFlipper.setFlipInterval(2000);
            mViewFlipper.startFlipping();

    mViewFlipper.setInAnimation(AnimationUtils.loadAnimation(getApplicationContext(), android.R.anim.slide_in_left));
    mViewFlipper.setOutAnimation(AnimationUtils.loadAnimation(getApplicationContext(), android.R.anim.slide_out_right));
}

protected String getNextImage() {
    if (index == imageURLs.size())
        index = 0;
    return imageURLs.get(index++);
}

}

1

There are 1 answers

0
tebitoq On BEST ANSWER

You should have something like that:

for(String url: imageURLs){
   ImageView image = new ImageView(getApplicationContext());

   UrlImageViewHelper.setUrlDrawable(image, url,R.drawable.loading);

  mViewFlipper.addView(image);
}

mViewFlipper.setAutoStart(true);
mViewFlipper.setFlipInterval(2000);
mViewFlipper.startFlipping();