活动不是封闭的类

时间:2017-05-23 20:26:32

标签: java android

public Object instantiateItem(ViewGroup container, int position) {
    Context context = FullImageActivity.this;   // i get error in this line that its not an enclosing class

我只是想将FullImageActivity调用到ImageAdapter类

ImageAdapter类

public   class ImageAdapter extends PagerAdapter {

    public String mImages[] =  {("http://www.fashionlady.in/wp-content/uploads/2016/03/creative-punjabi-mehndi-design-2016.jpg"),
            ("https://lumiere-a.akamaihd.net/v1/images/uk_toystory_chi_woody_n_5b5a006f.png?region=0,0,300,300"),
            ("https://lumiere-a.akamaihd.net/v1/images/open-uri20150422-20810-10n7ovy_9b42e613.jpeg"),
            ("http://www.wetpaint.com/wp-content/uploads/2015/11/toy-story-20th-anniversary.jpg"),
            ("http://i.imgur.com/4IZMjx3.jpg")};

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

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

    @Override
    public Object instantiateItem(ViewGroup container, int position) {

        Context context = FullImageActivity.this;
        ImageView imageView = new ImageView(context);

        imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        Picasso.with(context).load(mImages[position]).placeholder(R.drawable.loading2).error(R.drawable.nointernet).into(imageView);
        ((ViewPager) container).addView(imageView, 0);
        return imageView;
    }

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

这是我的FullImageActivity类

public class FullImageActivity extends AppCompatActivity {

    TouchImageView img;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle("ViewPager");

        setContentView(R.layout.activity_full_image);

        Intent i = getIntent();
        TouchImageView img = new TouchImageView(this);

        int position = i.getExtras().getInt("id");
        ImageAdapter adapter = new ImageAdapter();


        img = (TouchImageView) findViewById(R.id.img);
        img.setMaxZoom(4f);   
        Picasso.with(getApplicationContext()).load(adapter.mImages[position]).into(img);
        ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
        viewPager.setAdapter(adapter);
}

1 个答案:

答案 0 :(得分:1)

您的适配器类

public   class ImageAdapter extends PagerAdapter {

    Context activity_context;
    public String mImages[] =  {("http://www.fashionlady.in/wp-content/uploads/2016/03/creative-punjabi-mehndi-design-2016.jpg"),
            ("https://lumiere-a.akamaihd.net/v1/images/uk_toystory_chi_woody_n_5b5a006f.png?region=0,0,300,300"),
            ("https://lumiere-a.akamaihd.net/v1/images/open-uri20150422-20810-10n7ovy_9b42e613.jpeg"),
            ("http://www.wetpaint.com/wp-content/uploads/2015/11/toy-story-20th-anniversary.jpg"),
            ("http://i.imgur.com/4IZMjx3.jpg")};
    public ImageAdapter(Context context)
    {
            activity_context=context;
    }
    @Override
    public int getCount() {
        return mImages.length;
    }

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

    @Override
    public Object instantiateItem(ViewGroup container, int position) {


        ImageView imageView = new ImageView(activity_context);

        imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        Picasso.with(activity_context).load(mImages[position]).placeholder(R.drawable.loading2).error(R.drawable.nointernet).into(imageView);
        ((ViewPager) container).addView(imageView, 0);
        return imageView;
    }

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

您的活动课程

public class FullImageActivity extends AppCompatActivity {

    TouchImageView img;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle("ViewPager");

        setContentView(R.layout.activity_full_image);

        Intent i = getIntent();
        TouchImageView img = new TouchImageView(this);

        int position = i.getExtras().getInt("id");
        ImageAdapter adapter = new ImageAdapter(FullImageActivity.this);


        img = (TouchImageView) findViewById(R.id.img);
        img.setMaxZoom(4f);   
        Picasso.with(getApplicationContext()).load(adapter.mImages[position]).into(img);
        ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
        viewPager.setAdapter(adapter);
}