如何在viewpager中单击时在新活动中打开图像?

时间:2017-10-23 07:14:25

标签: java android android-viewpager

我使用viewpager来显示图片的幻灯片,但是当我点击任何一张图片时,需要在新活动中打开它。任何人都可以帮助我吗?

我点击图片时显示土司消息,但我的实际要求是在新活动中打开图片。

代码如下:

class MyCustomPagerAdapter extends PagerAdapter{
private Context context;
private int images[];
private LayoutInflater layoutInflater;
public int i;

MyCustomPagerAdapter(Context context, int images[]) {
    this.context = context;
    this.images = images;
    layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
    return images.length;
}

@Override
public boolean isViewFromObject(View view, Object object) {
    return view == object;
}

@Override
public Object instantiateItem(final ViewGroup container, final int position) {
    View itemView = layoutInflater.inflate(R.layout.full_profile, container, false);

    ImageView imageView = (ImageView) itemView.findViewById(R.id.expandedImage);
    imageView.setImageResource(images[position]);

    container.addView(itemView);

    //listening to image click
    imageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            //Toast.makeText(context, "you clicked image " + (position + 1), Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(context, ImageOpeningActivity.class);
            intent.putExtra("MY_IMAGE", images[position]);
            context.startActivity(intent);

        }
    });

    return itemView;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
    container.removeView((View) object);
}

}

ImageOpeningActivity.class

class ImageOpeningActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.imageopening);



    ImageView imageView = (ImageView) findViewById(R.id.myImageView);

    if(getIntent()!=null){
        int image  = getIntent().getExtras().getInt("MY_IMAGE");
        imageView.setImageResource(image);
    }
}

}

虽然我改变了我的代码但是收到错误,错误就在下面。

 java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.vmax.kalyanam/com.vmax.kalyanam.ImageOpeningActivity}: java.lang.IllegalAccessException: java.lang.Class<com.vmax.kalyanam.ImageOpeningActivity> is not accessible from java.lang.Class<android.app.Instrumentation>

public class Full_profile extends Activity {
Toolbar toolbar;
CollapsingToolbarLayout collapsingToolbarLayout;
//public String title = "KANDIBANDA ROJA";


protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.full_profile);


    ViewPager viewPager;
    int images[] = {
            R.drawable.one,
            R.drawable.two,
            R.drawable.three,
            R.drawable.one
    };

    MyCustomPagerAdapter myCustomPagerAdapter;
    viewPager = (ViewPager)findViewById(R.id.viewPager);
    myCustomPagerAdapter = new MyCustomPagerAdapter(Full_profile.this, images);
    viewPager.setAdapter(myCustomPagerAdapter);

4 个答案:

答案 0 :(得分:1)

首先在您的代码中执行此操作而不是Toast。注释Toast并粘贴这些行。

Intent intent = new Intent(this, ImageOpeningActivity.class);
intent.putExtra("MY_IMAGE", images[position]);
context.startActivity(intent);

第2步:使用ImageOpeningActivity的名称创建活动,并在其xml中使用Imageview,假设其Id为myImageView,然后在创建时执行以下操作

ImageView imageView = (ImageView) findViewById(R.id.myImageView);

    if(getIntent()!=null){
        int image  = getIntent().getExtras().getInt("MY_IMAGE");
        imageView.setImageResource(image);
    }

我假设您的图像[位置]是整数数组。

答案 1 :(得分:1)

将意图置于“听图像点击”

      imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               Intent intent = new Intent(this,PhotoViewerActivity.class);
intent.putExtra("image", imageurl);
    startActivity(intent);
            }
        });



public class PhotoViewerActivity extends Activity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); 


        String url = getIntent().getStringExtra("image");
     // you can show image from url using library like glide or picasso
    }

    @Override
    protected int getLayoutResourceId() {
        return R.layout.activity_photo_viewer;
    }

    @Override
    public void onBackPressed() {
        PhotoViewerActivity.this.finish();
    }
}

答案 2 :(得分:0)

使用xml布局中的imageview创建新活动,并从您的viewpager图像中单击调用

Intent intent = new Intent(this, ImageDetailActivity.class);

intent.putExtra("image",images[i]);
startActivity(intent);

ImageDetailActivity.java

    public class ImageDetailActivity extends AppCompatActivity {


     Drawable imageDrawable;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_image_detail);



          Intent intent =  getIntent();
              imageDrawable=   getResources().getDrawable(intent.getIntExtra("image",-1)); 
            imageView.setImageDrawable(imageDrawable);
        }   

    }

确保将此ImageDetailActivity添加到您的Android清单文件

答案 3 :(得分:0)

就像@Pratap所说,你可以将图片网址从一个活动发送到另一个活动。但是,如果您想将图像本身作为位图传递,请遵循此方法。 Bitmap实现了Parcelable,因此您可以将其作为intent extra传递。

Intent intent = new Intent(this, ImageDetailActivity.class);
intent.putExtra("BitmapImage", bitmap);

并通过调用

在ImageDetailActivity中检索它
Intent intent = getIntent(); 
Bitmap bitmap = (Bitmap) intent.getParcelableExtra("BitmapImage");

警告:不要通过Intent传递超过500Kb的数据 Refer this site