Implementing ViewPager in custom calendar

I have a FragmentActivity with 2 tabs. Each tab calls a fragment- Calendar and Converter:

public class Calendar extends SherlockFragmentActivity {

//create tabs
}


private class MyTabListener implements ActionBar.TabListener
{
    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        if(tab.getPosition()==0)
        {
            CalendarFragment frag = new CalendarFragment();
            ft.replace(android.R.id.content, frag);
        }
        else
        {
            ConverterFragment frag = new ConverterFragment();
            ft.replace(android.R.id.content, frag);
        }
    }
..

}

In the Calendar fragment, I would like to implement ViewPager for the Month view so that the user can swipe next/prev when changing the month.

This is the code I have till now. But all this does is display the current month calendar.

public class CalendarFragment extends Fragment implements OnClickListener {

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
       View v =  inflater.inflate(R.layout.simple_calendar_view, container, false);

       //set calendar adapter 
       //Code from http://w2davids.wordpress.com/android-simple-calendar/

       context = this.getActivity().getApplicationContext();
       CalendarPagerAdapter adapter = new CalendarPagerAdapter();
       myPager = (ViewPager) v.findViewById(R.id.pager);
       myPager.setAdapter(adapter);
       myPager.setCurrentItem(1);
    }


   public class CalendarPagerAdapter extends PagerAdapter {

         public int getCount() {
                 return NUM_AWESOME_VIEWS;
         }

         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 = Calendar.getInstance(Locale.getDefault());
                month = _calendar.get(Calendar.MONTH) + 1;
                year = _calendar.get(Calendar.YEAR);

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

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

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

                return v;
         }

         @Override
         public void destroyItem(View arg0, int arg1, Object arg2) {
                 ((ViewPager) arg0).removeView((View) arg2);

         }

         @Override
         public void finishUpdate(View arg0) {
                 // TODO Auto-generated method stub

         }

         @Override
         public boolean isViewFromObject(View arg0, Object arg1) {
                 return arg0 == ((View) arg1);

         }

         @Override
         public void restoreState(Parcelable arg0, ClassLoader arg1) {
                 // TODO Auto-generated method stub

         }

         @Override
         public Parcelable saveState() {
                 // TODO Auto-generated method stub
                 return null;
         }

         @Override
         public void startUpdate(View arg0) {
                 // TODO Auto-generated method stub

         }

     }

}

I am stuck here since I do not know how to display prev month calendar when the user swipes left and next month calendar when the user swipes right.

I have tried adding this code to Object instantiateItem:

 switch(position){
    case 0:
       Log.d("ViewPager", "Prev");
       if (month <= 1)
            {
                month = 12;
                year--;
            }
        else
            {
                month--;
            }
        if (hmonth <= 1)
            {
                hmonth = 12;
                hyear--;
            }
        else
            {
                hmonth--;
            }
        setGridCellAdapterToDate(month, year);
    break;
    case 2:

       Log.d("ViewPager", "Next");
       if (month > 11)
            {
                month = 1;
                year++;
            }
        else
            {
                month++;
            }
        if (hmonth > 11)
            {
                hmonth = 1;
                hyear++;
            }
        else
            {
                hmonth++;
            }
        setGridCellAdapterToDate(month, year);

    break;
    }

But no swiping happens. Where am I going wrong?

Answers


Sometimes touch event listeners don't play well together. Implementing onClickListenr for CalendarFragment may be stealing the touch event from your PagerAdapter.

------ EDIT -------

If Log.d("Object", "Instantiated"); is showing up in your debug log everytime you swipe, your PagerAdapter is working. This means that it is a problem with how you are changing the current month to display.

Try this:

// put this at the class level of calendarFragment.
Calendar holdTodaysDate = Calendar.getInstance(Locale.getDefault());


_calendar = Calendar.getInstance(Locale.getDefault());
_calendar = holdTodaysDate; // Add this line;
_calendar.add(Calendar.MONTH, position); // Add this line
month = _calendar.get(Calendar.MONTH) + 1;
year = _calendar.get(Calendar.YEAR);

This is to reset the calendar to today's date before adding position to it. The way it is now _calendar is maintaining it's state between views.


Need Your Help

Padding at the AVCaptureVideoData with kCVPixelFormatType_32BGRA

objective-c tcp camera avfoundation padding

I´m trying to send a image through tcp to a server, firts getting the buffer from the camera and then converting to grayScale the buffer, finally I send the buffer to the server.

Lazarus (FreePascal): How do I capture a system error and throw an exception?

exception-handling lazarus

I am trying to show an array of TPANELs to be used by the user as a menu system. It all seems to work fine but here's the issue.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.