android GridView ImageView 想实现点击图片 跳转后显示相应图片
packagecom.example.demo_stone_album;publicclassGridActivityextendsActivity{GridViewmG...
package com.example.demo_stone_album;
public class GridActivity extends Activity {
GridView mGridView;
ImageView big_imageview;
private String TAG = "GridActivity";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gridview);
Log.d(TAG, "stone one");
mGridView = (GridView)this.findViewById(R.id.gridview01);
big_imageview = (ImageView) findViewById(R.id.bigimageview);
mGridView.setBackgroundColor(Color.BLUE);
final Integer[] mId =
{
R.drawable.v1,
R.drawable.v2,
R.drawable.v3,
R.drawable.v4,
R.drawable.v5,
R.drawable.v6,
R.drawable.v7,
R.drawable.v8,
R.drawable.v9,
R.drawable.v10,
R.drawable.v11,
R.drawable.v12,
R.drawable.v13,
R.drawable.v14,
R.drawable.v15,
R.drawable.v16,
R.drawable.v17,
R.drawable.v18
};
mGridView.setAdapter(new BaseAdapter() {
@Override
public View getView(int position, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
ImageView imageview = new ImageView(GridActivity.this);
imageview.setLayoutParams(new GridView.LayoutParams(130, 130));
imageview.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageview.setImageResource(mId[position]);
return imageview;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mId.length;
}
});
mGridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
// TODO Auto-generated method stub
Log.v(TAG, ">>>>>>>>>stone<<<<<<<<");
int id = 0;
id = mId[position];
Intent intent = new Intent();
Bundle bundle = new Bundle();
bundle.putInt("address", id);
intent.putExtras(bundle);
intent.setClass(GridActivity.this, ImageViewActivity.class);
startActivity(intent);
}
});
test();
}
private void test(){
}
}
public class ImageViewActivity extends Activity{
private static final String TAG = "ImageViewActivity";
ImageView mBigImageView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.imageview);
final Integer[] mId =
{
R.drawable.v1,
......
R.drawable.v18
};
Intent intent = this.getIntent();
Bundle bundle = intent.getExtras();
int mItemId = bundle.getInt("address");
mBigImageView = (ImageView) findViewById(R.id.bigimageview);
mBigImageView = new ImageView(this);
mBigImageView.setImageResource(mId[mItemId]);
mBigImageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
}
} 展开
public class GridActivity extends Activity {
GridView mGridView;
ImageView big_imageview;
private String TAG = "GridActivity";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gridview);
Log.d(TAG, "stone one");
mGridView = (GridView)this.findViewById(R.id.gridview01);
big_imageview = (ImageView) findViewById(R.id.bigimageview);
mGridView.setBackgroundColor(Color.BLUE);
final Integer[] mId =
{
R.drawable.v1,
R.drawable.v2,
R.drawable.v3,
R.drawable.v4,
R.drawable.v5,
R.drawable.v6,
R.drawable.v7,
R.drawable.v8,
R.drawable.v9,
R.drawable.v10,
R.drawable.v11,
R.drawable.v12,
R.drawable.v13,
R.drawable.v14,
R.drawable.v15,
R.drawable.v16,
R.drawable.v17,
R.drawable.v18
};
mGridView.setAdapter(new BaseAdapter() {
@Override
public View getView(int position, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
ImageView imageview = new ImageView(GridActivity.this);
imageview.setLayoutParams(new GridView.LayoutParams(130, 130));
imageview.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageview.setImageResource(mId[position]);
return imageview;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mId.length;
}
});
mGridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
// TODO Auto-generated method stub
Log.v(TAG, ">>>>>>>>>stone<<<<<<<<");
int id = 0;
id = mId[position];
Intent intent = new Intent();
Bundle bundle = new Bundle();
bundle.putInt("address", id);
intent.putExtras(bundle);
intent.setClass(GridActivity.this, ImageViewActivity.class);
startActivity(intent);
}
});
test();
}
private void test(){
}
}
public class ImageViewActivity extends Activity{
private static final String TAG = "ImageViewActivity";
ImageView mBigImageView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.imageview);
final Integer[] mId =
{
R.drawable.v1,
......
R.drawable.v18
};
Intent intent = this.getIntent();
Bundle bundle = intent.getExtras();
int mItemId = bundle.getInt("address");
mBigImageView = (ImageView) findViewById(R.id.bigimageview);
mBigImageView = new ImageView(this);
mBigImageView.setImageResource(mId[mItemId]);
mBigImageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
}
} 展开
展开全部
mBigImageView.setImageResource(mId[mItemId]);这行错了!你传过来的是图片资源的id,怎么当成数组下标了!?
mBigImageView.setImageResource(mItemId);按道理应该这么写,但是你在两个activity里都存取了图片数组,就会生成不同的图片资源id,所以第一个activity里传过去的id有可能永远都没法在第二个activity里找到需要的图片
正确做法是:
用一个公共资源类存储这个图片数组,在这两个activity里直接引用就行,如:
public class Picture{
public static final int pic[] = {
R.drawable.v1,
R.drawable.v2,
R.drawable.v3,
R.drawable.v4,
R.drawable.v5
......
};
}
而且通常正确的做法时,点击gridview某一item时,直接把这个item的position传递过去就行,根据position在数组里找到图片,很省事
mBigImageView.setImageResource(mItemId);按道理应该这么写,但是你在两个activity里都存取了图片数组,就会生成不同的图片资源id,所以第一个activity里传过去的id有可能永远都没法在第二个activity里找到需要的图片
正确做法是:
用一个公共资源类存储这个图片数组,在这两个activity里直接引用就行,如:
public class Picture{
public static final int pic[] = {
R.drawable.v1,
R.drawable.v2,
R.drawable.v3,
R.drawable.v4,
R.drawable.v5
......
};
}
而且通常正确的做法时,点击gridview某一item时,直接把这个item的position传递过去就行,根据position在数组里找到图片,很省事
追问
是的,参数传错了,刚已经觉悟到了,非常感谢你的回答,图片放在一个公共资源类非常方便
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询