我有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);
}
});
}
}
謝謝編輯自己的帖子Cheesebaron –
'「然而,當我回去的GridView和挑選圖像3或4」'你能分享與此相關的代碼片段? – santalu
「Santalu我用我的手機在屏幕上的觸摸手指選擇圖像,但如果我選擇圖像3,例如在屏幕上滑動總是第一個圖像,而不選擇我選擇的圖像,但幻燈片很好地工作到左側或者正確。「 –