Answer the question
In order to leave comments, you need to log in
Why is the layout displayed in the fragment only on the first call?
Hello everyone, I'm using Navigation Drawer Activity at the same time as Tabbed Activity (created personally via SimpleFragmentPageAdapter).
Main Activity is used only once, and everything else is displayed in fragments (android studio 3.5.3, defaul templates)
So, when opening a fragment for the first time, all the information is in place as in the image:
Then we exit the fragment by going to another fragment and when opening fragment again, the information is completely missing, and you can’t switch between tabs (the position of the scroll bar can be in the center)
SimpleFragmentPagerAdapter
public class SimpleFragmentPagerAdapter extends FragmentPagerAdapter {
final int PAGE_COUNT = 2;
private Context mContext;
public SimpleFragmentPagerAdapter(FragmentManager fm, Context context) {
super(fm);
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 = mContext.getResources().getString(R.string.mileage_calculator);
final String paybackCalc = mContext.getResources().getString(R.string.payback_calculator);
final String[] tabTitles = {mileageCalc, paybackCalc};
return tabTitles[position];
}
}
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;
}
}
}
public class CalcFragment extends Fragment {
private Context mContext;
private Activity mActivity;
private CoordinatorLayout mCLayout;
private Toolbar mToolbar;
private TabLayout mTabLayout;
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
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;
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question