Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8973291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:24:48+00:00 2026-06-15T18:24:48+00:00

In my activity, I’m using ViewPager to swipe left/right and show month view accordingly.

  • 0

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

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T18:24:49+00:00Added an answer on June 15, 2026 at 6:24 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My Activity creates an AsyncTask that loads data from a file and updates the
My activity has a set of buttons on the left half of the screen
Activity that is sending the putExtra() @Override protected void onListItemClick(ListView l, View v, int
My activity has android:windowSoftInputMode=adjustResize and behaves accordingly in Android 2.1: Before soft keyboard appears
Ok activity 1 displays thumbnails of videos from the SD card in a gridVideoView.
Every activity within my android app is generated using HTML. When one of the
From activity A , I need to launch 3 Activities; B, C and D;
import android.app.Activity; import android.os.Bundle; import android.view.KeyEvent; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Button;
Hi i'm launching activity from preferences screen. Activity is shared among three preferences. I
I have an activity which extends PreferenceActivity. I'm loading preferences from the xml file.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.