我正在尝试在我的应用中创建一个图库,我现在将其作为网格视图,然后通过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;
}
}
答案 0 :(得分:1)
而不是图像视图将视图寻呼机放在FullImageActivity1
中this.mPagerfrimage = new pagerfrimage(getSupportFragmentManager(),
list of drawables here);
ViewPager vp_image = (ViewPager) findViewById(R.id.viewpager1);
vp_image.setAdapter(mPagerfrimage);
这是寻呼机适配器calss
class pagerfrimage extends FragmentPagerAdapter {
List<Integer> la;
public pagerfrimage(FragmentManager fm, List<Integer> result) {
super(fm);
this.la = result;
// TODO Auto-generated constructor stub
}
@Override
public Fragment getItem(int arg0) {
// TODO Auto-generated method stub
fragmentforimage ffrFragmentforimage = new fragmentforimage();
Bundle bundle = new Bundle();
bundle.putInt("resourceid", la.get(arg0));
bundle.putInt("number", arg0);
ffrFragmentforimage.setArguments(bundle);
return ffrFragmentforimage;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return la.size();
}
}
片段类
public class fragmentforimage extends Fragment {
int number;
int resourceid;
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
resourceid=getArguments()!=null?getArguments().getInt("resourceid"):0;
number=getArguments()!=null?getArguments().getInt("number"):0;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View v=inflater.inflate(R.layout.yourlayot containing image view, container, false);
ImageView iv=(ImageView)v.findViewById(R.id.your imageviews id);
Drawable drawable=getResources().getDrawable(resourceid);
iv.setImageDrawable(drawable);
return v;}
您可以传递当前所选的图片ID,这样您就可以根据需要首先更改代码