我是Android编程新手。 我正在创建一个非常简单的应用程序,其中我有超过5张静态图片(存储在drawable文件夹中)。现在,我创建了一个活动,它在gridView中显示所有图片。假设,如果我点击第二个位置图像,它将打开一个新活动,以全尺寸显示特定图像(使用ImageView)模式加上下一个/上一个按钮以在图像上导航(用户无需仅启动第一个图像,他/她可以从gridView中选择任何图像,然后导航将从该点本身开始)。 我的问题是,我不想对等于图像数量的开关盒数量进行编码,以便在按钮中导航。有没有其他方法来实现所需的方案?或者我可以用来实现同样的Android的任何其他功能?
提前致谢!!!
我也在添加我的代码(First Activity):
public class GridViewActivity extends Activity {@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_grid_view); GridView gridview = (GridView) findViewById(R.id.gridView); gridview.setAdapter(new ImageAdapter(this)); gridview.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View v, int position, long id) { //Create an Intent Intent intent = new Intent(getApplicationContext(), FullImageActivity.class); intent.putExtra("image", position); startActivity(intent); } }); }
}
Second Activity: public class FullImageActivity extends Activity implements OnClickListener {
Button button; int drawable; ImageView display; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_full_image); Intent intent = getIntent(); display = (ImageView) findViewById(R.id.fullImageView); ImageView imageView = null; String idName = "ivImage"; int id; for (int i = 1; i <= 2; i++) { id = getResources().getIdentifier(idName + i, "id", getPackageName()); imageView = (ImageView) findViewById(id); imageView.setTag((Integer) i); imageView.setOnClickListener(this); if (i == 1) { // make sure that your "drawable" has a value different than 0! // otherwise a click on setWall button might result into a crash drawable = id; } } int pos = intent.getExtras().getInt("image"); ImageAdapter imageAdapter = new ImageAdapter(this); imageView.setImageResource(imageAdapter.mThumbIds[pos]); Log.i("pos", "" + pos); // System.out.println("pos: " + pos); button = (Button) findViewById(R.id.btnNextScreen); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { InputStream inputStream = getResources().openRawResource( drawable); Bitmap bitmap = BitmapFactory.decodeStream(inputStream); try { getApplicationContext().setWallpaper(bitmap); } catch (IOException e) { // use Log.e() with all 3 params instead of // e.printStackTrace(); Log.e("MyTag", "couldn't set wallpaper", e); } } }); } @Override public void onClick(View v) { // just to make sure that the clicked view is a ImageView if (v instanceof ImageView) { ImageView imageView = (ImageView) v; int place = (Integer) imageView.getTag(); int drawableId = getResources().getIdentifier("image" + place, "drawable", getPackageName()); display.setImageResource(drawableId); drawable = drawableId; } }
}
我已经添加了@WarrenFaith建议我的代码。添加建议代码后,它在“imageView.setTag((Integer)i);”上给我一个错误。请告诉我我哪里出错了。