2012-09-14 45 views
0

我试图使用ViewSwitcher来像图片向导一样工作。带有Next和Prev按钮的ViewSwitcher

我的意思是会有下一个和上一个按钮来更改ViewSwitcher而不是图片库中的图像。我已经从Android示例应用程序的API Demo引用。

中,他们已经使用ViewSwitcherGallery,但我必须使用NextPrev按钮 代替。但我不知道该怎么做。

如示例应用程序,他们所使用

Gallery g = (Gallery) findViewById(R.id.gallery); 
g.setAdapter(new ImageAdapter(this)); 
g.setOnItemSelectedListener(this); 

ImageAdapter不断在自身居住在ViewSwitcher一个ImageView的添加新的图像。那么我怎样才能做到与下一个和上一个按钮相同?

Sample App Screen

回答

1

如果使用ImageSwitcher这是一个非常简单的事。你必须与你的两个Buttons更换Gallery,并将其链接到ImageSwitcher

private int[] mImageIds= //.. the ids of the images to use 
private int mCurrentPosition = 0; // an int to monitor the current image's position 
private Button mPrevious, mNext; // our two buttons 

两个buttons将有两个onClick回调:

public void goPrevious(View v) { 
    mCurrentPosition -= 1; 
    mViewSwitcher.setImageResource(mImageIds[mCurrentPosition]); 
    // this is required to kep the Buttons in a valid state 
    // so you don't pass the image array ids boundaries 
    if ((mCurrentPosition - 1) < 0) { 
     mPrevious.setEnabled(false); 
    } 
    if (mCurrentPosition + 1 < mImageIds.length) { 
     mNext.setEnabled(true); 
    } 
} 

public void goNext(View v) { 
    mCurrentPosition += 1; 
    mViewSwitcher.setImageResource(mImageIds[mCurrentPosition]); 
    // this is required to kep the Buttons in a valid state 
    // so you don't pass the image array ids boundaries 
    if ((mCurrentPosition + 1) >= mImageIds.length) { 
     mNext.setEnabled(false); 
    } 
    if (mCurrentPosition - 1 >= 0) { 
     mPrevious.setEnabled(true); 
    } 
} 

你必须记住禁用onCreate方法中的前Button(因为我们从数组中的第一个图像开始)。

相关问题