我的应用程序将图像显示为 gridview 并单击任何图像显示画廊视图允许使用 viewpager 左右滑动。我的代码如下,
public class MemorialPhotoAlbumViewpager extends FragmentActivity {
static Activity MemorialPhotoAlbumViewpagerActivity;
int positionstate = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String[] myStrings = intent.getStringArrayExtra("ArrayphotoAlbum");
int position = intent.getIntExtra("position", 0);
setContentView(R.layout.photo_album_viewpager);
// check the version of android
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy =
new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
ImagePagerAdapter adapter = new ImagePagerAdapter(myStrings,position);
viewPager.setAdapter(adapter);
}
private class ImagePagerAdapter extends PagerAdapter {
private String[] ArrayphotoAlbumb;
private int positionb;
public ImagePagerAdapter(String[] myStrings, int position)
{
ArrayphotoAlbumb = myStrings;
positionb = position;
}
@Override
public int getCount() {
return ArrayphotoAlbumb.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
Context context = MemorialPhotoAlbumViewpager.this;
ImageView imageView = new ImageView(context);
String test = ArrayphotoAlbumb[position];
Bitmap bimage= getBitmapFromURL(test);
imageView.setImageBitmap(bimage);
((ViewPager) container).addView(imageView, 0);
return imageView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
}
public static Bitmap getBitmapFromURL(String src) {
try {
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
我可以显示出来,但 viewpager 的位置总是从 0 开始。如果我点击 gridview 上的第二个图像并使用 viewpager 显示它,我怎么能允许向左滑动以显示第一个图像。