为什么我必须在自定义日历中滑动两次才能到达下个月?

时间:2018-12-01 07:14:15

标签: java android calendar

有人可以帮我吗?我确定这里的每个人都可以与我的情况有关。我已经连续三天盯着电脑了。我找不到解决方案。这让我疯狂!预先感谢您提供的任何赞赏的帮助。

问题:
    初始片段是当前月份(11月)。向右滑动即可转到下个月。向左滑动即可转到上个月。发生的是第一次滑动,即向左或向右滑动,显示了同一个月(11月)。然后,在下一次滑动时,一切都会按预期进行。

环境:4个班级。
    -CalendarHome-家长活动。
    -CalFragmentAdapter-片段适配器。
    -CalFragment-片段。
    -CalGridAdapter-GridView适配器。

CalendarHome:

FragmentManager fm=getSupportFragmentManager();

CalFragmentAdapter adapter=new CalFragmentAdapter(fm,cal);
    vp.setAdapter(adapter);
    vp.setCurrentItem(100,false);

    // Calendar month swipe actions
    vp.addOnPageChangeListener(new OnPageChangeListener(){
        @Override
        public void onPageSelected(int position){
            // If swipe left
            if(position<lastNum){
            cal.add(Calendar.MONTH,-1);
            // If swipe right
            }else if(position>lastNum){
            cal.add(Calendar.MONTH,1);
            }
            // Set the last month number to check against the next position number. Tells me the swipe direction.
            lastNum=position;
        }
    }

CalFragmentAdapter:

public CalFragmentAdapter(FragmentManager fm, Calendar cal) {
    super(fm);
    this.cal = cal;
}

@Override
public Fragment getItem(int position) {
    // Create fragment object
    CalFragment calFragmentOne = new CalFragment();
    // Pass data to fragment
    Bundle info = new Bundle();
    info.putSerializable("cal", cal);
    // More arguments go here
    calFragmentOne.setArguments(info);

    return calFragmentOne;
}

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

CalFragment:

    // Get data from adapter
    Calendar cal = (Calendar) getArguments().getSerializable("cal");

    // Create gridView adapter object.
    CalGridAdapter gridAdapter = new CalGridAdapter(context, cal);
    gridView.setAdapter(gridAdapter);

    return rootview;

CalGridAdapter:

public CalGridAdapter(Context context, Calendar cal) {
    this.context = context;
    this.calendarHeight = calendarHeight;

    // Get calendar variables values... all values are correct.
    this.month = cal.get(Calendar.MONTH);
    this.dateNum = cal.get(Calendar.DAY_OF_MONTH);

    // Calculate new values from calendar... all values are correct.
    CalendarHelper calendarHelper = new CalendarHelper(cal);
    begin = calendarHelper.getBeginDay();
    lastMonthEndNum = calendarHelper.getEndNum(-1);
    endNum = calendarHelper.getEndNum(0);
}

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

@Override
public Object getItem(int position) {
    return null;
}

@Override
public long getItemId(int position) {
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // Inflater object for individual cell layout
    LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    if (convertView == null) {
        // Inflate individual gridView cell layout
        view = new View(context);
        view = layoutInflater.inflate(R.layout.cal_single_date, null);

        // View objects
        TextView title_view = view.findViewById(R.id.title_view);
        dateView = view.findViewById(R.id.date_num_view);

        int cellNum = position + 1 - begin;
        // Set numbers of the previous month
        if (position < begin) {
            dateView.setText("" + (lastMonthEndNum - (begin - position) + 1));
        // Set numbers of the current month
        } else if (cellNum <= endNum) {
            dateView.setText("" + cellNum);
        // Set numbers of the next month
        } else if (position > endNum) {
            dateView.setText("" + (position - (endNum + begin) + 1));
        }
    }
    return view;
}

0 个答案:

没有答案