我想使日历像Google日历应用程序。我的意思是当用户滚动时它必须下个月调用,而向后滚动时它必须获得上个月。
因此,我在main_activity.xml中使用viewPager
,并从main_activity.class中调用包含CalendarFragment.class
的{{1}}。
但是,当我滚动recyclerView
Here is the google calendar picture我只希望它可滚动。
MainActivity
viewPager
日历片段
public class MainActivity extends AppCompatActivity {
ViewPager viewPager;
SlideAdapter slideAdapter;
HashSet<Date> events;
List calendarFragmentList = new ArrayList();
private Calendar currentDate = Calendar.getInstance();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager)findViewById(R.id.view_pager);
events = new HashSet<>();
events.add(new Date());
final CalendarFragment calendarFragment = new CalendarFragment();
calendarFragmentList.add(calendarFragment);
slideAdapter = new SlideAdapter(getSupportFragmentManager(), calendarFragmentList);
viewPager.setAdapter(slideAdapter);
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
private static final float thresholdOffset = 0.5f;
private boolean scrollStarted, checkDirection;
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
if (checkDirection) {
if (thresholdOffset > positionOffset) {
Log.i("onPageScrolled", "going left");
CalendarFragment calendarFragment = new CalendarFragment();
calendarFragment.currentDate.add(Calendar.MONTH, -1);
calendarFragment.updateCalendar();
calendarFragmentList.add(calendarFragment);
slideAdapter.notifyDataSetChanged();
} else {
Log.i("onPageScrolled", "going right");
}
checkDirection = false;
}
}
@Override
public void onPageSelected(int position) {
}
@Override
public void onPageScrollStateChanged(int state) {
if (!scrollStarted && state == ViewPager.SCROLL_STATE_DRAGGING) {
scrollStarted = true;
checkDirection = true;
} else {
scrollStarted = false;
}
}
});
}
}