I am currently making a app where students can view there lesson grid. The lessons are retrieved using a json file. Now the date changing is done via buttons in the actionbar, but i want to make it also work with smooth swipe effects.
The only things that need to change is the SlectedDate-1 on left swipe. the SelectedDate+1 on right swipe.
I tried it with ViewPager
s but it didn't work does anybody know how to do it?
Maybe something with 3 viewpagers?, please a detailed code because I'm not very familar with viewpagers code!
I've tried out Android ViewPager working with date code but it doesn't work for me.
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
String myFormat = "yyyy-MM-dd";
SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
switch (position) {
case 0:
// create your dates for the leftmost fragment
myCalendar.add(Calendar.DAY_OF_YEAR, -1);
final String dateje = sdf.format(myCalendar.getTime());
// mPager.setCurrentItem(1);
return ScreenSlidePageFragment.create(dateje);
case 1:
// create your dates for the center fragment
myCalendar.add(Calendar.DAY_OF_YEAR, 0);
final String dateje2 = sdf.format(myCalendar.getTime());
//mPager.setCurrentItem(1);
return ScreenSlidePageFragment.create(dateje2);
case 2:
myCalendar.add(Calendar.DAY_OF_YEAR, 1);
final String dateje3 = sdf.format(myCalendar.getTime());
//mPager.setCurrentItem(1);
return ScreenSlidePageFragment.create(dateje3);
}
return null;
}
@Override
public int getCount() {
return 3;
}
my example fragment
public class ScreenSlidePageFragment extends Fragment {
/**
* The argument key for the page number this fragment represents.
*/
public static final String ARG_PAGE = "page";
/**
* The fragment's page number, which is set to the argument value for {@link #ARG_PAGE}.
*/
private int mPageNumber;
/**
* Factory method for this fragment class. Constructs a new fragment for the given page number.
*/
public static ScreenSlidePageFragment create(String dateNumber) {
ScreenSlidePageFragment fragment = new ScreenSlidePageFragment();
Bundle args = new Bundle();
args.putString(ARG_PAGE, dateNumber);
fragment.setArguments(args);
return fragment;
}
public ScreenSlidePageFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPageNumber = 1;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout containing a title and body text.
ViewGroup rootView = (ViewGroup) inflater
.inflate(R.layout.fragment_screen_slide_page, container, false);
String date = this.getArguments().getString(ARG_PAGE);
// Set the title view to show the page number.
((TextView) rootView.findViewById(android.R.id.text1)).setText(date);
return rootView;
}
/**
* Returns the page number represented by this fragment object.
*/
public int getPageNumber() {
return mPageNumber;
}
}