【问题标题】:Calendar and PagerAdapter - Unlimited Views日历和 PagerAdapter - 无限次查看
【发布时间】:2012-10-24 11:33:41
【问题描述】:

在我的活动中,我使用 ViewPager 向左/向右滑动并相应地显示月视图。我将月份和年份值从 onCreate 传递给 PagerAdapter:

private static int NUM_AWESOME_VIEWS = 3;

viewPager = (ViewPager) v.findViewById(R.id.pager);
cAdapter = new CalendarPagerAdapter(this.getActivity().getApplicationContext(), date_value, fragmentManager, month, year);
viewPager.setAdapter(cAdapter);
viewPager.setOffscreenPageLimit(3);

日历从 11 月开始,然后滚动到 1 月。我希望日历向前和向后显示无限的视图。

PagerAdapter:

public CalendarPagerAdapter(Context context, int dv, FragmentManager fm, int month, int year){
    this.context = context;
    this.dv = dv;
    this.fm = fm;
    this.month = month;
    this.year = year;

}

public Object instantiateItem(View collection, int position) {
    Log.d("Object", "Instantiated");
    LayoutInflater inflater = (LayoutInflater) collection.getContext()
                     .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View v =  inflater.inflate(R.layout.simple_calendar_view, null);

    _calendar.set(Calendar.MONTH, month);
    _calendar.set(Calendar.YEAR, year);

    month = _calendar.get(Calendar.MONTH) + 1;
    year = _calendar.get(Calendar.YEAR);

    currentMonth = (Button) v.findViewById(R.id.currentMonth);
    currentMonth.setText(hijri.getMonthForInt(month-1).toUpperCase() + " " + year);

    calendarView = (GridView) v.findViewById(R.id.calendar);
    calendarWeek = (GridView) v.findViewById(R.id.calendarweek);

    calendarWeek.setAdapter(new CalendarWeekAdapter(context, firstDay));
    // Initialised
    adapter = new GridCellAdapter(calendarView, context, R.id.calendar_day_gridcell, month, year, 0, 0, 0);
    adapter.notifyDataSetChanged();
    calendarView.setAdapter(adapter);

    ((ViewPager) collection).addView(v, 0);

    return v;
}

基于Jon Willis' code,这是我想出来的,但是当viewpager的位置为0或2时,页面变为空白,然后显示下个月+1(即我滚动一月后, 页面闪烁并显示 March)。它滚动不流畅。我不知道该怎么做。

 viewPager.setOnPageChangeListener(new OnPageChangeListener() {
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {
            if (state == ViewPager.SCROLL_STATE_IDLE) 

                 Log.d("Calendar ViewPager", "Calendar ViewPager" + viewPager.getCurrentItem());

                    if (focusedPage == 0) {

                            _calendar.add(Calendar.MONTH, -1);
                            month = _calendar.get(Calendar.MONTH);
                            year = _calendar.get(Calendar.YEAR);

                            setGridCellAdapter(context, month, year);

                    } else if (focusedPage == 2) {

                           _calendar.add(Calendar.MONTH, +1);
                           month = _calendar.get(Calendar.MONTH);
                           year = _calendar.get(Calendar.YEAR);


                           setGridCellAdapter(context, month, year);

                    }

                    viewPager.setCurrentItem(1, false);
            }
    }

    @Override
    public void onPageSelected(int position) {
            focusedPage = position;
    }
});



public void setGridCellAdapter(Context ctx, int month, int year)
{
    cAdapter = new CalendarPagerAdapter(ctx, date_value, fragmentManager, month, year);
    Log.d("Object", "Re-instantiatted" + month + year);
    cAdapter.notifyDataSetChanged();
    viewPager.setAdapter(cAdapter);

}

【问题讨论】:

  • 我以为我们已经解决了这个问题。如果你能给我完整的项目文件夹,我会为你工作。你已经为此支付了我一次赏金。在使用 Google Drive 之前,我已经发布了完整的项目文件夹。我将其压缩,上传到谷歌驱动器,将其标记为公开,然后在此处发布指向它的链接。也就是说,如果您不介意让所有代码可用。
  • 嗨@JasonHessley,如果你能帮我解决这个问题,真的很感激。

标签: android calendar android-viewpager infinite


【解决方案1】:

我不确定我是否理解这一点(从未使用过这个 viewPager,而是一直使用另一个 Horizo​​ntalViewSlider),但是对于你的 onPageScrollStateChanged() 为什么你对focusedPage==0 和focusedPage==2 有特殊情况?

如果一月和三月没有什么特别之处,也许可以删除 onPageScrollStateChanged() 中的所有内容?

我的猜测是闪烁是从您调用 setGridCellAdapter() 时开始的,因为这将通过调用 notifyDataSetChanged 刷新页面。

【讨论】:

【解决方案2】:

我已经实现了这一点,但通过将每个页面用作 3 个片段来实现这一点并不完美。当用户 swift 向右或向左时,它会回到中间并为每个片段更新新的数据源。

在主要

MonthViewContentFragment[] fragList = new MonthViewContentFragment[3];
fragList[0] = MonthViewContentFragment.newInstance(prevMonth);
fragList[1] = MonthViewContentFragment.newInstance(currentMonth);
fragList[2] = MonthViewContentFragment.newInstance(nextMonth);

ViewPager monthPage = (ViewPager) v.findViewById(R.id.monthview_content);
MonthPageAdapter monthPageAdapter = new MonthPageAdapter(getFragmentManager(), fragList);
monthPage.setAdapter(monthPageAdapter);
monthPage.setOnPageChangeListener(this);
monthPage.setCurrentItem(1, false);

onPageScrollStateChanged

if (state == ViewPager.SCROLL_STATE_IDLE) {

    if (focusPage < PAGE_CENTER) {
        currentMonth.add(Calendar.MONTH, -1);
    } else if (focusPage > PAGE_CENTER) {
        currentMonth.add(Calendar.MONTH, 1);    
    }
    monthPageAdapter.setCalendar(currentMonth);
    monthPage.setCurrentItem(1, false);
    updateTitle();
}

onPageSelected

public void onPageSelected(int position) {
        // TODO Auto-generated method stub
        focusPage = position;
}

在 MonthPageAdapter 中

    MonthViewContentFragment[] fragList;    
    MonthViewContentFragment temp; 
    int fragNumber = 3;

    public MonthPageAdapter(FragmentManager fm, MonthViewContentFragment[] fragList) {
        super(fm);
        this.fragList = fragList;
    }

    @Override
    public int getItemPosition(Object object) {
        // TODO Auto-generated method stub
        return POSITION_NONE;
    }
    @Override
    public Fragment getItem(int position) {
        return fragList[position];      
    }

    @Override
    public int getCount() {
        return 3;
    }
    public void setCalendar(Calendar currentMonth) {

        Calendar prevMonth = Calendar.getInstance();
        prevMonth.setTime(currentMonth.getTime());
        prevMonth.add(Calendar.MONTH, -1);

        Calendar nextMonth = Calendar.getInstance();
        nextMonth.setTime(currentMonth.getTime());
        nextMonth.add(Calendar.MONTH, 1);
            //update all 3 fragments
        fragList[0].updateUI(prevMonth);
        fragList[1].updateUI(currentMonth);
        fragList[2].updateUI(nextMonth);
    }

在 MonthViewContentFragment 中

    @Override
    public void updateUI(Calendar monthYear) {

        View v = getView();
        if (v == null)
            return;
        //setNewDataSource calcurate new List<String> date
        calendarViewAdapter.setNewDataSource(monthYear);
        calendarViewAdapter.notifyDataSetChanged();
    }

这并不完美,因为当用户 swift 太快时。卷轴尚未变为空闲状态,然后ViewPager 到达终点。

【讨论】:

  • 感谢您的回复。你能告诉我PAGE_CENTERfocusPageisDragging是什么吗?
  • 我不知道为什么,但我收到了 ArrayIndexOutOfBoundException 错误。
  • PAGE_CENTER 始终为 1。focusPage 在 swifting 时设置为 0 或 2,然后在更新后设置回 1。 isDragging 只是删除它,我的错误。
  • @SaintTail,我也面临 viewpager 的问题,请帮助我。
  • 不错的解决方案。甚至@SaintTail 也没有上传所有代码,但这对我来说是一个很好的做法。谢谢。
猜你喜欢
  • 2018-08-14
  • 2013-08-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-13
相关资源
最近更新 更多