我是Android Studio中的新手
我正在Android Studio中构建有关GIRDVIEW IMAGE的程序。 我已经设置了2个布局: -layout1: activity_main.xml 以显示Girdview图像 -layout2: solo_picture.xml 仅显示一个Pictuter和1个Back按钮,以帮助在用户单击布局1时返回布局1 我有MainActivity.java和MyImageAdapter.java(附上代码) 这是一个问题:单击“后退”按钮时无法返回layout1。 虽然,我用:
//want to use-save state values
Bundle myOriginalMemoryBundle;
myOriginalMemoryBundle = savedInstanceState;
.....
//设置“返回”按钮以返回到layout1(GridView)
btnSoloBack = (Button) findViewById(R.id.btnSoloBack);
btnSoloBack.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// redraw the main screen showing the layout1
onCreate(myOriginalMemoryBundle);
}
});
调试时我不知道为什么myOriginalMemoryBundle = NULL。 之后,我的程序已停止,我无法返回layout1以继续活动... 请帮助您。 非常感谢<3 MainActivity.java
public class MainActivity extends AppCompatActivity {
//GUI control bound to screen1 (holding GidView)
GridView gridview;
//GUI controls bound to screen2 (holding single ImageView)
TextView txtSoloMsg;
ImageView imgSoloPhoto;
Button btnSoloBack;
//in case you want to use-save state values
Bundle myOriginalMemoryBundle;
String[] items = {"Photo-1", "Photo-2", "Photo-3", "Photo-4", "Photo-5",
"Photo-6", "Photo-7", "Photo-8", "Photo-9", "Photo-10", "Photo-11",
"Photo-12", "Photo-13", "Photo-14", "Photo-15",};
Integer[] thumbnails={R.drawable.pic01_small,R.drawable.pic02_small,R.drawable.pic03_small,
R.drawable.pic04_small,R.drawable.pic05_small,R.drawable.pic06_small,
R.drawable.pic07_small,R.drawable.pic08_small,R.drawable.pic09_small,
R.drawable.pic11_small,R.drawable.pic11_small,R.drawable.pic12_small,
R.drawable.pic13_small,R.drawable.pic14_small,R.drawable.pic15_small};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myOriginalMemoryBundle = savedInstanceState;
setContentView(R.layout.activity_main);
// setup GridView with its custom adapter and listener
gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new MyImageAdapter(this, thumbnails));
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
showBigScreen(position);
}
});
}//onCreate
private void showBigScreen(int position) {
// show the selected picture as a single frame in the second layout
setContentView(R.layout.solo_picture);
// plumbing – second layout
txtSoloMsg = (TextView) findViewById(R.id.txtSoloMsg);
imgSoloPhoto = (ImageView) findViewById(R.id.imgSoloPhoto);
// set caption-and-large picture
txtSoloMsg.setText(" Position= " + position + " " + items[position]);
imgSoloPhoto.setImageResource( thumbnails[position] );
// set GO BACK button to return to layout1 (GridView)
btnSoloBack = (Button) findViewById(R.id.btnSoloBack);
btnSoloBack.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// redraw the main screen showing the GridView
onCreate(myOriginalMemoryBundle);
}
});
}// showBigScreen
}//Activity
MyImageAdapter.java
public class MyImageAdapter extends BaseAdapter{
private Context context; // main activity’s context
Integer[] smallImages; // thumbnail data set
public MyImageAdapter(Context mainActivityContext,
Integer[] thumbnails) {
context =mainActivityContext;
smallImages = thumbnails;
}
// how many entries are there in the data set?
public int getCount() {
return smallImages.length;
}
// what is in a given 'position' in the data set?
public Object getItem(int position) {
return smallImages[position];
}
// what is the ID of data item in given 'position‘?
public long getItemId(int position) {
return position;
}
// create a view for each thumbnail in the data set
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
// if possible, reuse (convertView) image already held in cache
if (convertView == null) {
// no previous version of thumbnail held in the scrapview holder
// define entry in res/values/dimens.xml for grid height,width in dips
// <dimen name="gridview_size">100dp</dimen>
// setLayoutParams will do conversion to physical pixels
imageView = new ImageView(context);
int gridsize = context.getResources().getDimensionPixelOffset(R.dimen.gridview_size);
imageView.setLayoutParams(new GridView.LayoutParams(gridsize, gridsize));
//imageView.setLayoutParams(new GridView.LayoutParams(100, 100));//NOT a good practice
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setPadding(5, 5, 5, 5);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(smallImages[position]);
imageView.setId(position);
return imageView;
}//getView
}//MyImageAdapter
答案 0 :(得分:0)
您可以只为布局“ R.layout.solo_picture”定义另一个活动,而不必在同一活动中定义第二个布局。
然后只需将选定的图像数据从活动1解析到活动2。
当您从活动2退回到活动1时,Android将自动恢复以前的数据。
对不起,我英语不好。英语不是我的主要语言。
使用Intent分析活动之间的数据:
关于活动1:
Intent intent1 = new Intent(activity1.this, activity2.class);
intent1.putExtra("item", items[position]);
intent1.putExtra("thumbnail", thumbnails[position])
startActivity(intent1);
关于活动2:
Intent intent = getIntent();
String item = intent.getIntExtra("item", "");
String thumbnail = intent.getStringExtra("thumbnail", 0);
答案 1 :(得分:0)
为布局 solo_picture.xml 定义另一个活动。假设您有两个活动:MainActivity和Activity2,以及两个布局:activity_main.xml和solo_picture.xml。然后,在onCreate方法中编写以下代码。
MainActivity.java
setContentView(R.layout.activity_main);
Button button = (Button) findViewById((R.id.btn1));
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Activity2.class);
startActivity(intent);
}
});
Activity2.java
setContentView(R.layout.activity2); // R.layout.solo_picture in your case
Button button = (Button) findViewById(R.id.btn2);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Activity2.this, MainActivity.class);
startActivity(intent);
}
});
现在,您可以单击按钮2或按硬件的后退按钮来返回第一个布局。
请确保编辑您的 AndroidManifest.xml 文件。在此文件中添加Activity2。
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Activity2"></activity>