Why in the fragment layout is displayed only if the first request?

Hi all, am using Navigation Drawer Activity Tabbed Activity simultaneously (created personally through SimpleFragmentPageAdapter).
Main Activity used only once, and everything else appears in fragments (android studio 3.5.3, defaul templates)
Now, when you open the fragment first time, all the information in place as pictured:
5e3ed79103215550884616.png
Then leave the fragment and go to another fragment and fragment again, the information is completely absent, and switching between tabs does not work (position of the scroll bar may be centered)
5e3ed7ea2356b728332156.png
5e3ed822413ff515809082.png

SimpleFragmentPagerAdapter
public class SimpleFragmentPagerAdapter extends FragmentPagerAdapter {
 final int PAGE_COUNT = 2;
 private Context call mcontext;

 public SimpleFragmentPagerAdapter(FragmentManager fm, Context Context) {
super(fm);
 call mcontext = context;
}

@Override
 public Fragment getItem(int position) {
 switch (position) {
 case 0:
 MileageCalcFragment fragment1 = new MileageCalcFragment();
 return fragment1;

 case 1:
 PaybackCalcFragment fragment2 = new PaybackCalcFragment();
 return fragment2;
default:
 MileageCalcFragment fragment3 = new MileageCalcFragment();
 return fragment3;
}
}

@Override
 public int getCount() {
 return PAGE_COUNT;
}

@Override
 public CharSequence getPageTitle(int position) {
 final String mileageCalc = call mcontext.getResources().getString(R. string.mileage_calculator);
 final String paybackCalc = call mcontext.getResources().getString(R. string.payback_calculator);
 final String[] tabTitles = {mileageCalc, paybackCalc};
 return tabTitles[position];
}
}

MileageCalcFragment
public class MileageCalcFragment extends Fragment implements View.OnClickListener {

 private CalcViewModel calcViewModel;
 private ImageButton chooseCar;
 private ImageButton chooseVan;
 private ImageButton chooseTruck;
 private Button calculateMileage;
 private EditText getChargeAmount;
 private TextView resultMileageCalc;
 private int cartype;
 private double total;
 private double summ;
 private static final double[] fuelPrices = {0.70 /*CNG*/, 1.76 /*Diesel*/, 1.76 /*AI95*/, 1.66 /*AI92*/, 0.93 /*SUG*/};

 public MileageCalcFragment() {
 // Required empty public constructor
}


@Override
 public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
 Bundle savedInstanceState) {
 calcViewModel =
ViewModelProviders.of(this).get(CalcViewModel.class);
 View root = inflater.inflate(R. layout.fragment_mileage_calc, container, false);

 chooseCar = root.findViewById(R. id.chooseCar);
 chooseVan = root.findViewById(R. id.chooseVan);
 chooseTruck = root.findViewById(R. id.chooseTruck);
 getChargeAmount = root.findViewById(R. id.getChargeAmount);
 resultMileageCalc = root.findViewById(R. id.resultMileageCalc);

chooseCar.setOnClickListener(this);
chooseVan.setOnClickListener(this);
chooseTruck.setOnClickListener(this);

 calculateMileage = root.findViewById(R. id.calculateMileage);
 calculateMileage.setOnClickListener(new View.OnClickListener() {
@Override
 public void onClick(View View) {
 resultMileageCalc.setText(Double.toString(total + 100));

 Toast.makeText(getActivity().getApplicationContext(), "Cho", Toast.LENGTH_SHORT).show();
}
});

 return root;
}


@Override
 public void onClick(View View) {
 switch (cartype) {
 case R. id.chooseCar:
chooseCar.getBackground().setAlpha(100);
chooseVan.setBackgroundResource(R. drawable.imageview_red_border);
 final double[] carFuelRates = {8.7 /*CNG*/, 5.7 /*Diesel*/, 8.1 /*AI95*/, 8.1 /*AI92*/, 10.4 /*SUG*/};
 if (getChargeAmount.getText() == null) {
 summ = Double.parseDouble(getChargeAmount.getText().toString());
 } else {
 Toast.makeText(getActivity().getApplicationContext(), "The planned amount of fuel should be the number", Toast.LENGTH_LONG).show();
}
 for (int i = 0; i <= 5; i++) {
 double[] resArray = new double[5];
 resArray[i] = ((summ * 100) / (carFuelRates[i] * fuelPrices[i]));
 total = total + ((summ * 100) / (carFuelRates[i] * fuelPrices[i]));
}
break;
 case R. id.chooseVan:
chooseVan.getBackground().setAlpha(100);
chooseVan.setBackgroundResource(R. drawable.imageview_red_border);
 final double[] vanFuelRates = {18 /*CNG*/, 11.5 /*Diesel*/, 16 /*AI95*/, 16 /*AI92*/, 19.2 /*SUG*/};
 if (getChargeAmount.getText() == null) {
 summ = Double.parseDouble(getChargeAmount.getText().toString());
 } else {
 Toast.makeText(getActivity().getApplicationContext(), "The planned amount of fuel should be the number", Toast.LENGTH_LONG).show();
}
 for (int i = 0; i <= 5; i++) {
 double[] resArray = new double[5];
 resArray[i] = ((summ * 100) / (vanFuelRates[i] * fuelPrices[i]));
 total = total + ((summ * 100) / (vanFuelRates[i] * fuelPrices[i]));
}
break;
 case R. id.chooseTruck:
chooseTruck.getBackground().setAlpha(100);
chooseVan.setBackgroundResource(R. drawable.imageview_red_border);
 final double[] truckFuelRates = {32 /*CNG*/, 25 /*Diesel*/, 31 /*AI95*/, 31 /*AI92*/, 42 /*SUG*/};
 if (getChargeAmount.getText() == null) {
 summ = Double.parseDouble(getChargeAmount.getText().toString());
 } else {
 Toast.makeText(getActivity().getApplicationContext(), "The planned amount of fuel should be the number", Toast.LENGTH_LONG).show();
}
 for (int i = 0; i <= 5; i++) {
 double[] resArray = new double[5];
 resArray[i] = ((summ * 100) / (truckFuelRates[i] * fuelPrices[i]));
 total = total + ((summ * 100) / (truckFuelRates[i] * fuelPrices[i]));
}
break;
default:
 Toast.makeText(getActivity().getApplicationContext(), "Choose car type", Toast.LENGTH_SHORT).show();
break;
}
}
}

CalcFragment
public class CalcFragment extends Fragment {
 private Context call mcontext;
 private Activity mActivity;
 private CoordinatorLayout mCLayout;
 private Toolbar mToolbar;
 private mTabLayout TabLayout;
 private ViewPager mViewPager;
 private TabLayout tabLayout;
 private AppBarLayout mAppBarLayout;
 private CalcViewModel calcViewModel;

 public View onCreateView(@NonNull LayoutInflater inflater,
 ViewGroup container, Bundle savedInstanceState) {
 calcViewModel =
ViewModelProviders.of(this).get(CalcViewModel.class);
 View root = inflater.inflate(R. layout.fragment_calc, container, false);

 // Get the application context
 call mcontext = getActivity().getApplicationContext();
 mActivity = getActivity();

 // Get the widget reference from XML layout
 mCLayout = root.findViewById(R. id.coordinator_layout);
 mToolbar = getActivity().findViewById(R. id.toolbar);
 mTabLayout = root.findViewById(R. id.tab_layout);
 mViewPager = root.findViewById(R. id.view_pager);

 // Set a title for toolbar
mToolbar.setTitle(getResources().getString(R. string.menu_calc));

 // Set the support action bar
 ((AppCompatActivity) getActivity()).setSupportActionBar(mToolbar);

// ((AppCompatActivity)getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);

 // Initialize a new instance of PagerAdapter
 PagerAdapter adapter = new SimpleFragmentPagerAdapter(
getFragmentManager(),
mActivity
);

 // Set a PagerAdapter for ViewPager
mViewPager.setAdapter(adapter);

 // Setup the TabLayout with ViewPager
mTabLayout.setupWithViewPager(mViewPager);

 mAppBarLayout = getActivity().findViewById(R. id.mAppBarLayout);
mAppBarLayout.setElevation(0);

 return root;
}
}
April 7th 20 at 10:42
0 answer

Find more questions by tags Android