我有一个包含片段的Activity。是否有一种简单的方法来刷新片段?
我想过像getFragmentManager().beginTransaction().refresh().commit();
这样的东西......
但是没有什么能像刷新一样。有什么建议? My Fragment举着一个显示随机数的祝酒词。我想要反映片段,以便toast显示一个新的随机数,分别从Fragment.onCreateView()重做代码
public static class PlaceholderFragment extends Fragment {
TextView tv_question;
Button btn_answer_1;
Button btn_answer_2;
Button btn_answer_3;
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_activity_quiz, container, false);
Toast.makeText(getActivity(), String.valueOf(Math.random()), Toast.LENGTH_LONG).show();
tv_question = (TextView)rootView.findViewById(R.id.tv_question);
SupportFunctions.setViewBackgroundWithDrawable(tv_question, getResources().getDrawable(R.drawable.quiz_question_background));
btn_answer_1 = (Button)rootView.findViewById(R.id.btn_answer_1);
btn_answer_2 = (Button)rootView.findViewById(R.id.btn_answer_2);
btn_answer_3 = (Button)rootView.findViewById(R.id.btn_answer_3);
SupportFunctions.setViewBackgroundWithDrawable(btn_answer_1, getResources().getDrawable(R.drawable.quiz_answer_background));
SupportFunctions.setViewBackgroundWithDrawable(btn_answer_2, getResources().getDrawable(R.drawable.quiz_answer_background));
SupportFunctions.setViewBackgroundWithDrawable(btn_answer_3, getResources().getDrawable(R.drawable.quiz_answer_background));
btn_answer_1.setOnClickListener(btn_answer_listener);
btn_answer_2.setOnClickListener(btn_answer_listener);
btn_answer_3.setOnClickListener(btn_answer_listener);
// LOAD QUESTION FROM DB
Globals.setCORRECT_ANSWER(1);
// SHUFFLE ANSWERS
return rootView;
}
public OnClickListener btn_answer_listener = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int id = v.getId();
if (id == R.id.btn_answer_1 && Globals.getCORRECT_ANSWER() == 1) {
// correct
answerCorrect();
}
else if (id == R.id.btn_answer_2 && Globals.getCORRECT_ANSWER() == 2) {
// correct
answerCorrect();
}
else if (id == R.id.btn_answer_3 && Globals.getCORRECT_ANSWER() == 3) {
// correct
answerCorrect();
}
else {
answerWrong();
}
}
};
private void answerCorrect() {
Globals.incrementAnswersTotal(); // got to become sharedPreference
Globals.incrementAnswersRound();
Globals.incrementCorrectAnswersTotal(); // sharedPreference
Globals.incrementCorrectAnswersRound();
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setIcon(getResources().getDrawable(R.drawable.quiz_ic_dialog_correct));
builder.setTitle(getResources().getString(R.string.ad_correct_answer_heading));
builder.setMessage(getResources().getString(R.string.ad_correct_answer_text));
builder.setPositiveButton(R.string.ad_keep_on, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
getFragmentManager().beginTransaction().replace(R.id.container, getFragmentManager().findFragmentById(R.id.container)).commit();
}
});
builder.setNegativeButton(R.string.ad_to_menu, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
startActivity(new Intent(getActivity(), ActivityAppLaunch.class));
}
});
builder.setCancelable(false);
AlertDialog alert = builder.create();
alert.show();
}
private void answerWrong() {
Globals.incrementAnswersTotal(); // got to become sharedPreference
Globals.incrementAnswersRound();
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setIcon(getResources().getDrawable(R.drawable.quiz_ic_dialog_wrong));
builder.setTitle(getResources().getString(R.string.ad_wrong_answer_heading));
builder.setMessage(getResources().getString(R.string.ad_wrong_answer_text) + "mein Text hier");
builder.setPositiveButton(R.string.ad_keep_on, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
builder.setNegativeButton(R.string.ad_to_menu, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
startActivity(new Intent(getActivity(), ActivityAppLaunch.class));
}
});
builder.setCancelable(false);
AlertDialog alert = builder.create();
alert.show();
}
}
答案 0 :(得分:0)
正如Ben Pearson所说,不是将所有代码放在onCreateView()中,而是制作另一种方法:
public void showNewRandomNumber() {
Toast.makeText(getActivity(), String.valueOf(Math.random()), Toast.LENGTH_LONG).show();
}
然后从任何地方打电话给它。听起来你想在positiveButton的onClick方法中调用它,这样就可以了(如果是这样的话,那么showNewRandomNumber就不必具有公共可见性)。