返回活动时选择正确的图像视图

时间:2017-08-17 18:59:18

标签: android android-gridview

我有带图像的GridView,我可以向左和向右滑动没问题。但是,当我返回到GridView并选择图像3或4.在我滑动的活动中,它总是返回到图像2而不是我选择的图像。

如何修复代码以滚动到正确的图像?

public class CustomSwip extends PagerAdapter {

    private int[] imageResource = { R.drawable.pic_2, R.drawable.pic_3,
            R.drawable.pic_4, R.drawable.pic_5, R.drawable.pic_6, R.drawable.pic_7,
            R.drawable.pic_8, R.drawable.pic_9, R.drawable.pic_10, R.drawable.pic_11,
            R.drawable.pic_12, R.drawable.pic_13, R.drawable.pic_14, R.drawable.pic_15,};

    private ImageView imageView;
    private Context ctx;
    private LayoutInflater layoutInflater;
    private ImageAdapter adapter;

    public CustomSwip(Context c) {
        ctx = c;
    }

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

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        layoutInflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View itemView = layoutInflater.inflate(R.layout.activity_custom_swip, container, false);
        ImageView imageView = (ImageView) itemView.findViewById(R.id.swip_image_view);
        TextView textView = (TextView) itemView.findViewById(R.id.imageCount);
        imageView.setImageResource(imageResource[position]);
        textView.setText("id" + position);

        container.addView(itemView);
        return itemView;
    }

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

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

FullImageActivity.Class

public class FullImageActivity extends AppCompatActivity {



    ViewPager viewPager;
    CustomSwip customSwipe;





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


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

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

        viewPager=(ViewPager)findViewById(R.id.viewPager);
        customSwipe=new CustomSwip(this);
        viewPager.setAdapter(customSwipe);




    }


}

ImageAdapter类

public class ImageAdapter extends BaseAdapter {

    private Context context;

    public Integer[] images = new Integer[]{
            R.drawable.pic_2,
            R.drawable.pic_3, R.drawable.pic_4,
            R.drawable.pic_5, R.drawable.pic_6,


    };

    public ImageAdapter(Context c) {
        context = c;
    }


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

    @Override
    public Object getItem(int position) {
        return images[position];
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView = new ImageView(context);
        imageView.setImageResource(images[position]);
        imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        imageView.setLayoutParams(new GridView.LayoutParams(350, 250));
        return imageView;


    }


}

MainActivityClass

public class MainActivity extends AppCompatActivity {



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



        GridView gridView = (GridView) findViewById(R.id.gridView);
        gridView.setAdapter(new ImageAdapter(this));

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent i = new Intent(getApplicationContext(), FullImageActivity.class);
                i.putExtra("id", position);
                startActivity(i);

            }
        });





    }
}

1 个答案:

答案 0 :(得分:1)

您可以使用setCurrentItem(int position)方法从所需位置启动查看寻呼机。

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


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

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

    viewPager=(ViewPager)findViewById(R.id.viewPager);
    customSwipe=new CustomSwip(this);
    viewPager.setAdapter(customSwipe);
    //we are setting the target position here
    viewPager.setCurrentItem(position);

}