2 myc 100 myc_100 于 2013.09.09 16:49 提问

Gridview 点击图像显示成gallery view

程序显示图像如 gridview,点击任何图像显示 gallery view,使用 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);
    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 显示,允许向左滑动来显示第一个图像。

1个回答

shaojie519
shaojie519   2013.09.09 17:08

viewpager.setCurrentItem(int index) 就行了

Csdn user default icon
上传中...
上传图片
插入图片