我正在尝试在我的应用程序中制作图像库,我目前将其作为网格视图,然后通过 ImageAdapter 传递到完整图像视图。
问题是我希望能够在从网格视图单击后在图片之间滑动。
有人提到我可能可以使用片段来做到这一点,但我是 android 开发的新手。
有人可以进一步解释如何做到这一点。谢谢
我的代码是:
public class Marble extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.marble);
GridView gridView = (GridView) findViewById(R.id.gridview1);
// Instance of ImageAdapter Class
gridView.setAdapter(new ImageAdapter1(this));
/**
* On Click event for Single Gridview Item
* */
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
// Sending image id to FullScreenActivity
Intent i = new Intent(getApplicationContext(), FullImageActivity1.class);
// passing array index
i.putExtra("id", position);
startActivity(i);
}
});
}
}
然后
public class FullImageActivity1 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");
ImageAdapter1 imageAdapter1 = new ImageAdapter1(this);
ImageView imageView = (ImageView) findViewById(R.id.fullimage);
imageView.setImageResource(imageAdapter1.mThumbIds[position]);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
}
}
最后:
public class ImageAdapter1 extends BaseAdapter {
private Context mContext;
// Keep all Images in array
Integer[] mThumbIds = {
R.drawable.arabescato, R.drawable.biancocarrara,
R.drawable.botticinoclassico, R.drawable.calacattaoro,
R.drawable.cremamarfil, R.drawable.cremavalencia,
R.drawable.emperadordark, R.drawable.jurabeige,
R.drawable.neromarquina, R.drawable.perlatoolympo,
R.drawable.rojoalicante
};
// Constructor
public ImageAdapter1(Context c){
mContext = c;
}
@Override
public int getCount() {
return mThumbIds.length;
}
@Override
public Object getItem(int position) {
return mThumbIds[position];
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(mContext);
imageView.setImageResource(mThumbIds[position]);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(265, 265));
return imageView;
}
}