无法显示点击的图像

时间:2014-03-01 21:10:22

标签: android android-viewpager

我在gridview中显示的图像很少。我想要的是当我点击该图像时,它应该全屏显示。我也让viewpager有滑动功能。我遇到的问题是当我点击图像时显示第一个图像(不是那个)。

他是我的主要活动

public class Ppdtsample extends Activity {
GridView grid;

 static int[] imageId = { R.drawable.ppdt1, R.drawable.ppdt2,
        R.drawable.ppdt3, R.drawable.ppdt4, R.drawable.ppdt5,
        R.drawable.ppdt6, R.drawable.ppdt7, };

  @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ppdtsamples);

        ImageAdapter adapter = new ImageAdapter(Ppdtsample.this, imageId);
       grid=(GridView)findViewById(R.id.grid_view);
                grid.setAdapter(adapter);

                grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {


                    @Override
                    public void onItemClick(AdapterView<?> parent, View view,
                            int position, long id) {
                        // TODO Auto-generated method stub
                         Intent i = new Intent(getApplicationContext(), FullImageActivity.class);
                            // passing array index
                            i.putExtra("id", position);

                            startActivity(i);
                    }
                });
    }

}

这是我使用滑动功能全屏显示的活动:

public class FullImageActivity extends Activity {






@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.full_image);

    // get intent data
    Intent i = getIntent();

    // Selected image id
  int position = i.getExtras().getInt("id");

    // passing array index

    ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);


    viewPager.setBackgroundResource(0);





    SlideImage adapter = new SlideImage(this);
    viewPager.setAdapter(adapter);


}
}

这是我的viewpager类

public class SlideImage extends PagerAdapter {
Context context;


private int[] GalImages = { R.drawable.ppdt1, R.drawable.ppdt2,
        R.drawable.ppdt3, R.drawable.ppdt4, R.drawable.ppdt5,
        R.drawable.ppdt6, R.drawable.ppdt7, };


SlideImage(Context context) {
    this.context = context;
}

@Override
public int getCount() {
    return GalImages.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(context);
    int padding = context.getResources().getDimensionPixelSize(R.dimen.activity_vertical_margin);
    imageView.setPadding(padding, padding, padding, padding);
    imageView.setScaleType(ImageView.ScaleType.FIT_XY);


    imageView.setImageResource(GalImages[position]);
    ((ViewPager) container).addView(imageView, 0);
    return imageView;
}

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

它显示的是第一张图片,而不是我点击的图片,因为我无法通过点击图片的“位置”。它从SlideImage.class

中的此方法获取位置
Object instantiateItem(ViewGroup container, int position)

这是第一个位置。我想将位置从fullImageActivity.class传递到此行中的位置

imageView.setImageResource(GalImages[position]);

在SlideshowActivity上。

这可能看起来很复杂但我只想将位置值从FullImageActivity.class传递给SlideImage.class。请帮帮我

1 个答案:

答案 0 :(得分:0)

您的Ppdtsample看起来很奇怪。我试图修改您的Ppdtsample课程,这对我有用:

public class Ppdtsample extends Activity {
GridView grid;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.grid_layout);

    grid = (GridView) findViewById(R.id.grid_view);

    // Instance of SlideImage Class
    gridView.setAdapter(new SlideImage(this));

    gridView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View v,
                int position, long id) {

            Intent i = new  Intent(getApplicationContext(),FullImageActivity.class);

            i.putExtra("id", position);
            startActivity(i);
        }
    });
  }
}

您无法传递ID的原因是因为您在Ppdtsample中设置了错误的适配器。代码中应该是SlideImage,而不是ImageAdapter