没有为选项卡调用onCreateView

时间:2017-06-11 17:30:39

标签: java android tabs

我的标签有一个viewpager。我有三个选项卡,我在第一个选项卡中进行计算,将它存储在tinyDb中,它仍然是sharedPreferences。我还在第二个选项卡中进行了另一个计算并将答案存储在tinydb中,然后我将这些答案存储在单击按钮上,第三个选项卡应根据这两个答案进行计算。我发现计算工作完美,但问题是这个。当我完成应用程序应该在选项卡一中计算的内容时,我会转到选项卡二,并且当我转到选项卡三时也应该计算它应该计算的onCreateView for tab三不会被调用所以答案不会更新直到我回去选项卡一,然后我再次单击选项卡三,然后调用onCreateView。我尝试过将setOffscreenPageLimit设置为0,但无济于事请问我该如何解决这个问题。

这是标签一的代码

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View rootView = inflater.inflate(R.layout.fragment_year1__timeline, container, false);
    circularProgressBar = (CircularProgressBar)rootView.findViewById(R.id.circularProgressbarTimelineYr1);
    textView = (TextView)rootView.findViewById(R.id.textForPercentageTimeLineYr1);
    listView = (ListView) rootView.findViewById(R.id.listForTimelineYr1);
    generalArray = new ArrayList<>();
    tinyDB = new TinyDB(getContext());
    storedList = tinyDB.getListString("savedSubjects");
    storedArray = tinyDB.getListString("savedSubjects2");
    saveBtn = (Button)rootView.findViewById(R.id.btnSaveYearOneFirstSemester);


    generalArray.addAll(storedList);
    generalArray.addAll(storedArray);
    tinyDB.putListString("generalArray", generalArray);
    final ArrayList<String>generalArrayStore = tinyDB.getListString("generalArray");
    arrayAdapter = new CustomListAdapterForCgpa(getActivity(), generalArray, new IOnGradeSelectedListener() {
        @Override
        public void onGradeSelected(String key, int value) {

            try
            {
                int studentGatheredPoints = 0;
                int totalMaximumSize = 0;


                for (String course : generalArrayStore) {
                    int courseGradeValue = getCourseGradeValue(course);
                    courseLoadValue = getCourseLoadValue(course);

                    int sum = courseGradeValue * courseLoadValue;

                    studentGatheredPoints += sum;

                    totalMaximumSize += courseLoadValue* 5;//change to the value of total edittext
                }

                int percent = studentGatheredPoints * 100 / totalMaximumSize;
                circularProgressBar.setProgress(percent);

                DecimalFormat decimalFormat = new DecimalFormat("0.00");
                double total = 100;
                double multiply = percent*5.00;
                finalAnswer = multiply/total;
                textView.setText(decimalFormat.format(finalAnswer));

            }catch (ArithmeticException ae)
            {
                String userName = tinyDB.getString("Username");
                new SweetAlertDialog(getContext(), SweetAlertDialog.NORMAL_TYPE)
                        .setTitleText("Haha, "+userName + " don't be in a hurry")
                        .setContentText("Credit loads have to be in your courses before viewing your timeline")
                        .setConfirmText("OK")
                        .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                            @Override
                            public void onClick(SweetAlertDialog sweetAlertDialog) {
                                sweetAlertDialog.dismissWithAnimation();
                                Intent intent = new Intent(getContext(), Splanner.class);
                                startActivity(intent);
                            }
                        })
                        .show();
            }

            textView.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                }

                @Override
                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                    saveBtn.setVisibility(View.VISIBLE);


                }

                @Override
                public void afterTextChanged(Editable editable) {

                }
            });

            saveBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    saveBtn.setVisibility(View.INVISIBLE);
                    tinyDB.putDouble("finalAnswerYr1FirstSemester",finalAnswer);
                }
            });





        }
    });

    listView.setAdapter(arrayAdapter);
    arrayAdapter.notifyDataSetChanged();

    return rootView;
}

private int getCourseGradeValue(String courseName) {
    String key = courseName + "_selectedValue";
    return tinyDB.getInt(key);
}

private int getCourseLoadValue(String courseName) {
    String key = courseName + "_selectedCourseLootValue";

    return tinyDB.getInt(key);
}

第二个标签

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View rootView =  inflater.inflate(R.layout.fragment_year2__timeline_second_semester, container, false);

    circularProgressBar = (CircularProgressBar)rootView.findViewById(R.id.circularProgressbarTimelineYr2_2);
    textView = (TextView)rootView.findViewById(R.id.textForPercentageTimeLineYr2_2);
    listView = (ListView)rootView.findViewById(R.id.listForTimelineYr2_2);
    tinyDB = new TinyDB(getContext());
    saveBtn = (Button)rootView.findViewById(R.id.btnSaveYearTwoSecondSemesterTimeline);

    storedList = tinyDB.getListString("storedList2ndYr2");
    arrayAdapter = new CustomListAdapterForCgpa(getActivity(), storedList, new IOnGradeSelectedListener() {
        @Override
        public void onGradeSelected(String key, int value) {

            try {
                int studentGatheredPoints = 0;
                int totalMaximumSize = 0;


                for (String course : storedList) {
                    int courseGradeValue = getCourseGradeValue(course);
                    courseLoadValue = getCourseLoadValue(course);

                    int sum = courseGradeValue * courseLoadValue;

                    studentGatheredPoints += sum;

                    totalMaximumSize += courseLoadValue* 5;//change to the value of total edittext
                }

                int percent = studentGatheredPoints * 100 / totalMaximumSize;
                circularProgressBar.setProgress(percent);

                DecimalFormat decimalFormat = new DecimalFormat("0.00");
                double total = 100;
                double multiply = percent*5.00;
                finalAnswer = multiply/total;
                textView.setText(decimalFormat.format(finalAnswer));

            }catch (ArithmeticException ae)
            {
                String userName = tinyDB.getString("Username");
                new SweetAlertDialog(getContext(), SweetAlertDialog.NORMAL_TYPE)
                        .setTitleText("Haha, "+userName + " don't be in a hurry")
                        .setContentText("Credit loads have to be in your courses before viewing your timeline")
                        .setConfirmText("OK")
                        .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                            @Override
                            public void onClick(SweetAlertDialog sweetAlertDialog) {
                                sweetAlertDialog.dismissWithAnimation();
                                Intent intent = new Intent(getContext(), Splanner.class);
                                startActivity(intent);
                            }
                        })
                        .show();
            }


        }
    });

    textView.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                saveBtn.setVisibility(View.VISIBLE);
        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            saveBtn.setVisibility(View.VISIBLE);

        }

        @Override
        public void afterTextChanged(Editable editable) {

        }
    });

    saveBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            saveBtn.setVisibility(View.INVISIBLE);
            String getFinalAnswer = textView.getText().toString();
            double saveFinalAnswer = Double.parseDouble(getFinalAnswer);
            tinyDB.putDouble("finalAnswerYr2SecondSemester",saveFinalAnswer);
        }
    });
    listView.setAdapter(arrayAdapter);
    arrayAdapter.notifyDataSetChanged();


    return rootView;
}

private int getCourseGradeValue(String courseName) {
    String key = courseName + "_selectedValue";
    return tinyDB.getInt(key);
}

private int getCourseLoadValue(String courseName) {
    String key = courseName + "_selectedCourseLootValue";

    return tinyDB.getInt(key);
}

@Override
public void onResume() {
    super.onResume();
}

}

第三个

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View rootView =  inflater.inflate(R.layout.fragment_year2__final_timeline, container, false);
    circularProgressBar = (CircularProgressBar)rootView.findViewById(R.id.circularProgressbarFinalTimelineYr2);
    textView = (TextView)rootView.findViewById(R.id.textForPercentageFinalTimelineYr2);
    tinyDB = new TinyDB(getContext());

    double yearOneFinalGpFirstSemester = tinyDB.getDouble("finalAnswerYr2FirstSemester", 0.00);
    double yearOneFinalGpSecondSemester = tinyDB.getDouble("finalAnswerYr2SecondSemester", 0.00);
    double finalGp = yearOneFinalGpFirstSemester+yearOneFinalGpSecondSemester;
    double finalGpFinal = finalGp/2;
    double val = finalGpFinal;
    val = val*100;
    val = (double)((int) val);
    val = val /100;
    textView.setText(String.valueOf(val));

    int total = 5;
    int change = (int) finalGpFinal;
    int multiply = 100*change;
    int divide = multiply/total;
    circularProgressBar.setProgress(divide);

    tinyDB.putDouble("FinalGpYear2", finalGpFinal);
    System.out.println(yearOneFinalGpFirstSemester);


    return rootView;
}

@Override
public void onResume() {
    super.onResume();

}}

1 个答案:

答案 0 :(得分:0)

最好在您的活动中使用页面更改侦听器,并在页面更改时更新UI内容。

        viewPager.setAdapter(fragmentPagerAdapter);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                   //Get current fragment and update UI
                   BaseFragment fragment= (BaseFragment) fragmentPagerAdapter.getItem(position);
                //assumes updateUI ,a method in BaseFragment is overriden in Fragment implementation to update UI.You can place your logic here.
                fragment.updateUI();
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });