我的应用程序将图像显示为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显示它,则允许我向左滑动以显示第一个图像。
答案 0 :(得分:1)
如果要为视图寻呼机启动新活动,请从gridview获取所选图像的位置并将其传递给该活动。然后,您可以在视图寻呼机中设置所选图像,如
pager.setCurrentItem(currentid); // here current id is the position of selected image.