I am using FragmentViewPagerAdapter to create a calendar Activity. Each fragment represents a day, and I have a Switch on my main activity that allows the user to filter results on each fragment. I am using ViewModel to request data, so when the switch is checked I need to do a new request to my server to get new data for each fragment, My problem is that when I do my request, the day sent is next fragment's date, not the current's one.
I already tried to limit of screen pages to 0 but nothing changed.
public class PoiPlanningFragment extends Fragment implements InitApplicationListener{
MutableLiveData<Boolean> filtered = new MutableLiveData<>();
private Context mContext;
DateTime startTime;
DateTime endTime;
switcher = getActivity().findViewById(R.id.app_switcher);
public PoiPlanningFragment() {
// Required empty public constructor
}
public static Fragment getInstance(int position, String date, String resourceId, String resourceType,boolean filter) {
Bundle bundle = new Bundle();
bundle.putInt("pos", position );
bundle.putString("date", date);
bundle.putString(ARG_RESOURCE_ID, resourceId);
bundle.putString(ARG_RESOURCE_TYPE, resourceType);
PoiPlanningFragment tabFragment = new PoiPlanningFragment();
tabFragment.setArguments(bundle);
return tabFragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
DateTimeFormatter horaire = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm");
startTime = horaire.parseDateTime(date.concat(" " + STARTHOUR));
endTime = horaire.parseDateTime(date.concat(" " + ENDHOUR));
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_poi_planning, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
//Switch my meetings
switcher.setOnCheckedChangeListener(new SwitchClickedListener(this.filtered));
//Observes whether the user has a filter on own meetings or not
filtered.observe(this, filtered -> {
//here startTime always shows next tab startTime and endTime
BookingRepository.getUserPlanning(planning,resourceId,Utility.toTimeStamp(startTime),Utility.toTimeStamp(endTime), filtered);
loader.setVisibility(View.VISIBLE);
});
}
}
private class SwitchClickedListener implements CompoundButton.OnCheckedChangeListener{
MutableLiveData<Boolean> listener;
SwitchClickedListener(MutableLiveData<Boolean> listener){
this.listener = listener;
}
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
//Set filter to true
this.listener.setValue(b);
}
}
public class PoiPlanningViewPagerAdapter extends FragmentStatePagerAdapter {
private final static String TAG = PoiPlanningViewPagerAdapter.class.getName();
private List<Date> dateList;
private Context context;
private String poiId;
private String resouceType;
public PoiPlanningViewPagerAdapter(FragmentManager fm, List<Date> dates, String poidId,String resourceType, Context context) {
super(fm);
this.dateList = dates;
this.context = context;
this.poiId = poidId;
this.resouceType = resourceType;
}
@Override
public Fragment getItem(int position) {
Date datetime = dateList.get(position);
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
String date = df.format(datetime);
PoiPlanningFragment planningFragment = (PoiPlanningFragment) PoiPlanningFragment.getInstance(position, date, poiId,resouceType,false);
return planningFragment;
}
@Override
public int getCount() {
return dateList.size();
}
@Override
public CharSequence getPageTitle(int position) {
Date datetime = dateList.get(position);
DateFormat df = new SimpleDateFormat(context.getString(R.string.date_format));
String date = df.format(datetime);
return date;
}
}
In the previous code, if I check the switch and I am on Monday 02 11th It will load data of Tuesday.