2

I have a ViewPager that represents weeks and its infinite.

The problem is when I swipe left or right the right data loads and a second later reloads data from the week that it started with.

PageModelWeek.java

public class PageModelWeek
{
    private int index;

    private String text;    

    private String monday;
    private String mondayValue;
    private String tuesday;
    private String tuesdayValue;
    private String wednesday;
    private String wednesdayValue;
    private String thursday;
    private String thursdayValue;
    private String friday;
    private String fridayValue;
    private String saturday;
    private String saturdayValue;
    private String sunday;
    private String sundayValue;

    private Context context;

    public PageModelWeek(int index, Context context)
    {
        this.index = index;
        this.context = context;
        setIndex(index);
    }

    public int getIndex()
    {
        return index;
    }

    public void setIndex(int index)
    {
        this.index = index;
        setWeek(index);
    }

    public String getText()
    {
        return text;
    }

    public void setText(String text)
    {
        this.text = text;
    }   

    public Context getContext()
    {
        return context;
    }

    public void setContext(Context context)
    {
        this.context = context;
    }

    private void setWeek(int index)
    {
        SimpleDateFormat dayOfWeekFormat = new SimpleDateFormat("EEEE", Locale.getDefault());   
        SimpleDateFormat dayOfMonthFormat = new SimpleDateFormat("dd", Locale.getDefault());

        Calendar calendar = Calendar.getInstance();

        calendar.add(Calendar.WEEK_OF_YEAR, index); 
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        calendar.set(Calendar.HOUR_OF_DAY, 0);

        for (int i = 0; i < 7; i++)
        {           
            if (i == 0)
            {
                monday = dayOfWeekFormat.format(calendar.getTime()).substring(0, 1).toUpperCase();
                mondayValue = dayOfMonthFormat.format(calendar.getTime());
            }
            else if (i == 1)
            {
                tuesday = dayOfWeekFormat.format(calendar.getTime()).substring(0, 1).toUpperCase();
                tuesdayValue = dayOfMonthFormat.format(calendar.getTime());
            }
            else if (i == 2)
            {
                wednesday = dayOfWeekFormat.format(calendar.getTime()).substring(0, 1).toUpperCase();
                wednesdayValue = dayOfMonthFormat.format(calendar.getTime());
            }
            else if (i == 3)
            {
                thursday = dayOfWeekFormat.format(calendar.getTime()).substring(0, 1).toUpperCase();
                thursdayValue = dayOfMonthFormat.format(calendar.getTime());
            }
            else if (i == 4)
            {
                friday = dayOfWeekFormat.format(calendar.getTime()).substring(0, 1).toUpperCase();
                fridayValue = dayOfMonthFormat.format(calendar.getTime());
            }
            else if (i == 5)
            {
                saturday = dayOfWeekFormat.format(calendar.getTime()).substring(0, 1).toUpperCase();
                saturdayValue = dayOfMonthFormat.format(calendar.getTime());
            }
            else if (i == 6)
            {
                sunday = dayOfWeekFormat.format(calendar.getTime()).substring(0, 1).toUpperCase();
                sundayValue = dayOfMonthFormat.format(calendar.getTime());
            } 

            calendar.add(Calendar.DATE, 1);
        }       

        SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
        String d = sdf.format(calendar.getTime());              

        this.text = d;      
    }

    public String getMonday()
    {
        return monday;
    }

    public String getMondayValue()
    {
        return mondayValue;
    }

    public String getTuesday()
    {
        return tuesday;
    }

    public String getTuesdayValue()
    {
        return tuesdayValue;
    }

    public String getWednesday()
    {
        return wednesday;
    }

    public String getWednesdayValue()
    {
        return wednesdayValue;
    }

    public String getThursday()
    {
        return thursday;
    }

    public String getThursdayValue()
    {
        return thursdayValue;
    }

    public String getFriday()
    {
        return friday;
    }

    public String getFridayValue()
    {
        return fridayValue;
    }

    public String getSaturday()
    {
        return saturday;
    }

    public String getSaturdayValue()
    {
        return saturdayValue;
    }

    public String getSunday()
    {
        return sunday;
    }

    public String getSundayValue()
    {
        return sundayValue;
    }       
}

The listener

viewPagerWeeks.setOnPageChangeListener(new OnPageChangeListener()
        {

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

            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2)
            {}

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

                    final PageModelWeek leftPage = mWeekPageModel[PAGE_LEFT];
                    final PageModelWeek middlePage = mWeekPageModel[PAGE_MIDDLE];
                    final PageModelWeek rightPage = mWeekPageModel[PAGE_RIGHT];

                    final int oldLeftIndex = leftPage.getIndex();
                    final int oldMiddleIndex = middlePage.getIndex();
                    final int oldRightIndex = rightPage.getIndex();

                    // user swiped to right direction --> left page
                    if (mSelectedWeekPageIndex == PAGE_LEFT)
                    {

                        // moving each page content one page to the right
                        leftPage.setIndex(oldLeftIndex - 1);
                        middlePage.setIndex(oldLeftIndex);
                        rightPage.setIndex(oldMiddleIndex);

                        setWeekContent(PAGE_RIGHT);
                        setWeekContent(PAGE_MIDDLE);
                        setWeekContent(PAGE_LEFT);

                        // user swiped to left direction --> right page
                    }
                    else if (mSelectedWeekPageIndex == PAGE_RIGHT)
                    {

                        leftPage.setIndex(oldMiddleIndex);
                        middlePage.setIndex(oldRightIndex);
                        rightPage.setIndex(oldRightIndex + 1);

                        setWeekContent(PAGE_LEFT);
                        setWeekContent(PAGE_MIDDLE);
                        setWeekContent(PAGE_RIGHT);
                    }

                    viewPagerWeeks.setCurrentItem(PAGE_MIDDLE, false);
                }
            }
        });

setContentWeek

private void setWeekContent(int index)
    {
        final PageModelWeek model = mWeekPageModel[index];                  
    }

Adapter

private class ViewPagerWeeksAdaper extends PagerAdapter
    {

        @Override
        public int getItemPosition(Object object)
        {
            return POSITION_NONE;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object)
        {
            container.removeView((View) object);
        }

        @Override
        public int getCount()
        {
            // we only need three pages
            return 3;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position)
        {
            LinearLayout ll = (LinearLayout) mInflater.inflate(R.layout.dayview_header_days, null);         
            PageModelWeek currentWeek = mWeekPageModel[position];       

            for (int i = 0; i < ll.getChildCount(); i++)
            {
                LinearLayout llDay = (LinearLayout) ll.getChildAt(i);
                TextView tvDayOfWeek = (TextView) llDay.getChildAt(0);
                TextView tvDayOfMonth = (TextView) llDay.getChildAt(1);

                if (i == 0)
                {
                    tvDayOfWeek.setText(currentWeek.getMonday());
                    tvDayOfMonth.setText(currentWeek.getMondayValue());
                }
                else if (i == 1)
                {
                    tvDayOfWeek.setText(currentWeek.getTuesday());
                    tvDayOfMonth.setText(currentWeek.getTuesdayValue());
                }
                else if (i == 2)
                {
                    tvDayOfWeek.setText(currentWeek.getWednesday());
                    tvDayOfMonth.setText(currentWeek.getWednesdayValue());
                }
                else if (i == 3)
                {
                    tvDayOfWeek.setText(currentWeek.getThursday());
                    tvDayOfMonth.setText(currentWeek.getThursdayValue());
                }
                else if (i == 4)
                {
                    tvDayOfWeek.setText(currentWeek.getFriday());
                    tvDayOfMonth.setText(currentWeek.getFridayValue());
                }
                else if (i == 5)
                {
                    tvDayOfWeek.setText(currentWeek.getSaturday());
                    tvDayOfMonth.setText(currentWeek.getSaturdayValue());
                }
                else if (i == 6)
                {
                    tvDayOfWeek.setText(currentWeek.getSunday());
                    tvDayOfMonth.setText(currentWeek.getSundayValue());
                }               
            }

            container.addView(ll);
            return ll;
        }

        @Override
        public boolean isViewFromObject(View view, Object obj)
        {
            return view == obj;
        }
    }

And this is called when I start the Activity

private void initWeekPageModel()
    {
        for (int i = 0; i < mWeekPageModel.length; i++)
        {
            // init the pagemodel with indexes of -1, 0 and 1
            mWeekPageModel[i] = new PageModelWeek(i - 1, this);
        }
    }
AndroidCoder
  • 157
  • 1
  • 12

1 Answers1

0

I think this line is whats bugging you located in onPageScrollStateChanged(int state):

 viewPagerWeeks.setCurrentItem(PAGE_MIDDLE, false);

As I read the code. It will perform some actions as soon as the viewpager "settles" and then in the end scroll to the initial page (which is the middle page I assume).

cYrixmorten
  • 7,032
  • 3
  • 22
  • 33
  • Thanks for your reply. However that line ensures that the ViewPager is infinite. I move data around on the pages and it only loops through 3 pages. If I don't have that line the VP will only slide one right and one left. – AndroidCoder Feb 13 '14 at 11:36
  • As you generate views based on page position, I would suggest you use the method by petrnohejl in http://stackoverflow.com/questions/7766630/changing-viewpager-to-enable-infinite-page-scrolling for infinite scrolling instead. That solution also keeps the code isolated inside the pageadapter. This though requires you (I think) to use FragmentStatePagerAdapter, thus switching to fragments for view-handling instead of your current instantiateItem. I know it seems like a lot of work but it will most likely be worth it. – cYrixmorten Feb 14 '14 at 07:23
  • For starters you could try and remove the code for infinite viewpaging and see if the problem persists. – cYrixmorten Feb 14 '14 at 07:41