在标题片段中添加日期

时间:2017-12-13 02:17:00

标签: android android-fragments android-viewpager slide

我有片段的综合浏览量。

这些片段被添加到通过屏幕上的幻灯片移动管理网页浏览的适配器中。每次用户滑动左侧,并且他在最后一个片段上,创建新片段并添加到适配器/网页视图。

问题是,我希望每个片段的标题是星期几和日期,就像日历一样。问题是,当我进入那一周的最后一天时,我得到两个具有相同标题的片段:

enter image description here 我的代码:

Date date = new Date(); //在我的onCreate方法之前

private void setupViewPager(ViewPager myViewPager) {

        SimpleDateFormat sdf = new SimpleDateFormat("EEE - d MMM");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);

        if(adapter.getCount() % 7 == 0){
            date = calendar.getTime();
            String day = sdf.format(date);
            adapter.addFragment(new Diafragment(), day);
        }
        else if (adapter.getCount() % 7 == 1){
            calendar.add(Calendar.DATE, 1);
            date = calendar.getTime();
            String dayplusone = sdf.format(date);
            adapter.addFragment(new Diafragment(), dayplusone);
        }
        else if (adapter.getCount() % 7 == 2){
            calendar.add(Calendar.DATE, 1);
            date = calendar.getTime();
            String dayplustwo = sdf.format(date);
            adapter.addFragment(new Diafragment(), dayplustwo);
        }
        else if (adapter.getCount() % 7 == 3){
            calendar.add(Calendar.DATE, 1);
            date = calendar.getTime();
            String dayplustree = sdf.format(date);
            adapter.addFragment(new Diafragment(), dayplustree);
        }
        else if (adapter.getCount() % 7 == 4){
            calendar.add(Calendar.DATE, 1);
            date = calendar.getTime();
            String dayplusfour = sdf.format(date);
            adapter.addFragment(new Diafragment(), dayplusfour);
        }
        else if (adapter.getCount() % 7 == 5){
            calendar.add(Calendar.DATE, 1);
            date = calendar.getTime();
            String dayplusfive = sdf.format(date);
            adapter.addFragment(new Diafragment(), dayplusfive);
        }
        else if (adapter.getCount() % 7 == 6){
            calendar.add(Calendar.DATE, 1);
            date = calendar.getTime();
            String dayplussix = sdf.format(date);
            adapter.addFragment(new Diafragment(), dayplussix);
        }

        myViewPager.setAdapter(adapter);
        myViewPager.setCurrentItem(adapter.getCount());
    }

public void addFragment(Fragment fragment, String title) {
    mFragments.add(fragment);
    mFragmentTitles.add(title);
}

0 个答案:

没有答案