日历和PagerAdapter - 无限浏览 [英] Calendar and PagerAdapter - Unlimited Views

查看:266
本文介绍了日历和PagerAdapter - 无限浏览的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的活动,我使用ViewPager刷卡左/右相应的显示和月视图。我是从的onCreate 传递月份和年份的PagerAdapter:

 私有静态诠释NUM_AWESOME_VIEWS = 3;

viewPager =(ViewPager)v.findViewById(R.id.pager);
cAdapter =新CalendarPagerAdapter(this.getActivity()getApplicationContext(),DATE_VALUE,fragmentManager,月,年);
viewPager.setAdapter(cAdapter);
viewPager.setOffscreenPageLimit(3);
 

日历始于十一月,然后滚动至一月。我想日历显示无限的意见前进和后退。

PagerAdapter:

 公共CalendarPagerAdapter(上下文的背景下,INT DV,FragmentManager FM,月,年整型){
    this.context =背景;
    this.dv = DV;
    this.fm = FM;
    this.month =月;
    this.year =年;

}

公共对象instantiateItem(查看收集,INT位置){
    Log.d(对象,实例化);
    LayoutInflater充气=(LayoutInflater)collection.getContext()
                     .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    视图V = inflater.inflate(R.layout.simple_calendar_view,NULL);

    _calendar.set(Calendar.MONTH,月);
    _calendar.set(Calendar.YEAR,一年);

    一个月= _calendar.get(Calendar.MONTH)+ 1;
    年= _calendar.get(Calendar.YEAR);

    currentMonth =(按钮)v.findViewById(R.id.currentMonth);
    currentMonth.setText(hijri.getMonthForInt(月-1).toUpperCase()++年);

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

    calendarWeek.setAdapter(新CalendarWeekAdapter(上下文,firstDay));
    //初始化
    适配器=新GridCellAdapter(calendarView,上下文,R.id.calendar_day_gridcell,月,年,0,0,0);
    adapter.notifyDataSetChanged();
    calendarView.setAdapter(适配器);

    ((ViewPager)集合).addView(V,0);

    返回伏;
}
 

基于乔恩·威利斯'code

,这就是我来了,但是当所述viewpager的位置为0或2,页变为空白,然后显示下月+ 1(即,后余滚动一月,页面闪烁,并显示3月)。不能平稳滚动。我不知道怎么回事,要做到这一点。

  viewPager.setOnPageChangeListener(新OnPageChangeListener(){
    @覆盖
    公共无效onPageScrolled(INT位置,浮positionOffset,诠释positionOffsetPixels){

    }

    @覆盖
    公共无效onPageScrollStateChanged(INT状态){
            如果(国家== ViewPager.SCROLL_STATE_IDLE)

                 Log.d(日历ViewPager,日历ViewPager+ viewPager.getCurrentItem());

                    如果(focusedPage == 0){

                            _calendar.add(Calendar.MONTH,-1);
                            月= _calendar.get(Calendar.MONTH);
                            年= _calendar.get(Calendar.YEAR);

                            setGridCellAdapter(上下文,月,年);

                    }否则如果(focusedPage == 2){

                           _calendar.add(Calendar.MONTH,+ 1);
                           月= _calendar.get(Calendar.MONTH);
                           年= _calendar.get(Calendar.YEAR);


                           setGridCellAdapter(上下文,月,年);

                    }

                    viewPager.setCurrentItem(1,假);
            }
    }

    @覆盖
    公共无效onPageSelected(INT位置){
            focusedPage =位置;
    }
});



公共无效setGridCellAdapter(上下文CTX,INT月,年整型)
{
    cAdapter =新CalendarPagerAdapter(CTX,DATE_VALUE,fragmentManager,月,年);
    Log.d(对象,重新instantiatted+月+年);
    cAdapter.notifyDataSetChanged();
    viewPager.setAdapter(cAdapter);

}
 

解决方案

我必须做到实现这一点,但不是完全用在每一页的3个片段。当用户迅速向左或向右,它会重新设置为中,更新的新数据源的每个片段。

在主

  MonthViewContentFragment [] fragList =新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 =新MonthPageAdapter(getFragmentManager(),fragList);
monthPage.setAdapter(monthPageAdapter);
monthPage.setOnPageChangeListener(本);
monthPage.setCurrentItem(1,假);
 

onPageScrollStateChanged

 如果(国家== ViewPager.SCROLL_STATE_IDLE){

    如果(focusPage< PAGE_CENTER){
        currentMonth.add(Calendar.MONTH,-1);
    }否则如果(focusPage> PAGE_CENTER){
        currentMonth.add(Calendar.MONTH,1);
    }
    monthPageAdapter.setCalendar(currentMonth);
    monthPage.setCurrentItem(1,假);
    updateTitle();
}
 

onPageSelected

 公共无效onPageSelected(INT位置){
        // TODO自动生成方法存根
        focusPage =位置;
}
 

在MonthPageAdapter

  MonthViewContentFragment [] fragList;
    MonthViewContentFragment温度;
    INT fragNumber = 3;

    公共MonthPageAdapter(FragmentManager FM,MonthViewContentFragment [] fragList){
        超(FM);
        this.fragList = fragList;
    }

    @覆盖
    公众诠释getItemPosition(Object对象){
        // TODO自动生成方法存根
        返回POSITION_NONE;
    }
    @覆盖
    公共片段的getItem(INT位置){
        返回fragList [位置]
    }

    @覆盖
    公众诠释getCount将(){
        返回3;
    }
    公共无效setCalendar(日历currentMonth){

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

        日历nextMonth = Calendar.getInstance();
        nextMonth.setTime(currentMonth.getTime());
        nextMonth.add(Calendar.MONTH,1);
            //更新所有3个片段
        fragList [0] .updateUI(prevMonth);
        fragList [1] .updateUI(currentMonth);
        fragList [2] .updateUI(nextMonth);
    }
 

在MonthViewContentFragment

  @覆盖
    方法public void updateUI(日历monthYear){

        视图V = getView();
        如果(V == NULL)
            返回;
        // setNewDataSource calcurate新的名单,其中,字符串>日期
        calendarViewAdapter.setNewDataSource(monthYear);
        calendarViewAdapter.notifyDataSetChanged();
    }
 

这是不完美的,因为当用户迅速太快。滚动不改变闲置但随后 ViewPager 到达其终点。

In my activity, I'm using ViewPager to swipe left/right and show month view accordingly. I'm passing the month and year values from onCreate to the 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);

The Calendar starts from November and then scrolls till January. I would like the Calendar to display unlimited views forward and backward.

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;
}

Based on Jon Willis' code, this is what I have come up with, but when the position of the viewpager is 0 or 2, the page goes blank and then displays the next month + 1 (that is, after I scroll January, the page flickers and displays March). It does not scroll smoothly. I am not sure how else to do it.

 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);

}

解决方案

I have achieve to implement this but not perfectly by using each page as 3 fragments. When User swift right or left, It will set back to middle and update new data source for each fragment.

In Main

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);

in 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();
}

in onPageSelected

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

In 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);
    }

In 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();
    }

This is not perfect because when user swift too fast. The scroll is not change to idle yet then ViewPager reach its end.

这篇关于日历和PagerAdapter - 无限浏览的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆