重新加载片段就好像屏幕会旋转一样

时间:2014-03-22 02:25:11

标签: android fragment reload

我建立了一个测验,在我的片段中显示了问题+答案。 每次用户选择答案时,都会显示一个对话框,用户可以决定是否继续播放。目前我对此作出如下反应:

@Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                startActivity(new Intent(getActivity(), ActivityQuiz.class));
                getActivity().overridePendingTransition(0, 0);
            }

但这似乎很慢。我宁愿重新加载片段,就像屏幕旋转一样。 我怎么能这样做?

这是代码的其余部分:

public static class PlaceholderFragment extends Fragment {

    TextView tv_question;
    Button btn_answer_1;
    Button btn_answer_2;
    Button btn_answer_3;
    JSONArray jArray = null;

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_activity_quiz, container, false);

        ...
        btn_answer_1.setOnClickListener(btn_answer_listener);
        btn_answer_2.setOnClickListener(btn_answer_listener);
        btn_answer_3.setOnClickListener(btn_answer_listener);

        ...

        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.INCREMENT_ANSWERS_ROUND();
        //Globals.incrementCorrectAnswersTotal(); // sharedPreference
        Globals.INCREMENT_CORRECT_ANSWERS_ROUND();

        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) + "\n\n" + Globals.getREMARK());
        builder.setPositiveButton(R.string.ad_keep_on, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                startActivity(new Intent(getActivity(), ActivityQuiz.class));
                getActivity().overridePendingTransition(0, 0);
            }

        });
        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.INCREMENT_ANSWERS_ROUND(); 

        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) + "\"" + Globals.getCORRECT_ANSWER_TEXT() + "\"" + "\n\n" + Globals.getREMARK());
        builder.setPositiveButton(R.string.ad_keep_on, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                startActivity(new Intent(getActivity(), ActivityQuiz.class));
                getActivity().overridePendingTransition(0, 0);
            }

        });
        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();

    }

}

1 个答案:

答案 0 :(得分:1)

onCreateView(...)中执行所有操作可能并不理想。您最好的选择是将与加载问题相关的所有内容移动到自己的方法中。在onCreateView(...)中,膨胀您的视图并存储对它们的本地引用。然后,覆盖其中一个保证在此之后被调用的片段生命周期方法,例如onActivityCreated()并从那里调用加载方法;这将得到问题并用正确的数据填充视图。然后,只要您想要一个新问题,只需调用该加载方法以使用新问题和答案重新填充视图。如果需要,我可以提供伪代码。

编辑:这是一些说明我的意思的伪代码。这绝不是有效的代码,但它是你应该做的事情的基础。

class Fragment {
    private TextView question
    private TextView answer

    onCreateView {
        View v = inflate view
        question = view.findView(question)
        answer = view.findView(answer)
    }

    onActivityCreated {
        loadQuestionAndAnswer()
    }

    loadQuestionAndAnswer {
        JSON json = Interwebs.getJSON()
        question.setText(json.getQuestion())
        answer.setText(json.getAnswer())
    }

    Dialog.onClick {
        loadQuestionAndAnswer()
    }
}