我有一個包含片段的活動。有沒有簡單的方法來刷新片段? 我想過像getFragmentManager().beginTransaction().refresh().commit();
... 但這裏沒有什麼喜歡刷新。有什麼建議麼?我的片段舉行了一個顯示隨機數的敬酒。我想refreh片段所以敬酒分別顯示了一個新的隨機數從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();
}
}
你是什麼意思的'刷新'和你的用例是什麼?包含代碼。 –
再次加載它。我的片段提供了一個隨機數,我想刷新它,所以顯示一個新的隨機數。看到上面的代碼。我需要它在我的'positiveButton' – user3432220
我沒有看到任何隨機數字和沒有positiveButton。正確的代碼在哪裏?你爲什麼不生成一個新的隨機數? new Random()。nextInt(); – ElDuderino