Android Open Source - Timetable Event Pager






From Project

Back to project page Timetable.

License

The source code is released under:

GNU General Public License

If you think the Android project Timetable listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.timetable.android;
//from  w w  w.  j a  v  a2 s  . c  o  m
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;

import org.holoeverywhere.LayoutInflater;
import org.holoeverywhere.widget.TextView;

import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ScrollView;

import com.timetable.android.activities.EventDayViewActivity;

/*
 * Class, that instantiate views of events for each day, and allows user to slide among the days.
 */
public class EventPager extends ViewPager {

  private final EventDayViewActivity mActivity;

  private Date mInitDate; 
  
  private final static int INIT_PAGE_NUMBER = 1000;
  
  private EventPagerAdapter mEventPagerAdapter;
  
  private EventViewProvider mEventViewProvider; 
  
  public EventPager(EventDayViewActivity activity, Date initDate) {
    super(activity);
    setId(1000);
    mActivity = activity;
    mInitDate = initDate;
    
    setOffscreenPageLimit(0);
    LayoutInflater layoutInflater = LayoutInflater.from(activity);  
    layoutInflater.inflate(R.layout.event_pager, this, true);
    mEventViewProvider = EventViewProvider.getInstance(mActivity);
    mEventPagerAdapter = new EventPagerAdapter(initDate);
    setAdapter(mEventPagerAdapter);
    setOnPageChangeListener(activity.getEventPagerListener());
    
  }
  
  /*
   * Should be could after EventPager is created to display default date. 
   */
  public void prepare() {
    setCurrentItem(INIT_PAGE_NUMBER);
  }
  
  /*
   * Return currently displayed date.
   */
  public Date getDisplayedDate() {
    return getDateByPageNumber(getCurrentItem());
  }
  
  public Date getDateByPageNumber(int pageNumber) {
    Date date = new Date();
    date.setTime(mInitDate.getTime() + (long) (pageNumber-INIT_PAGE_NUMBER)*24*3600*1000);
    return date;
  }
  
  private int getPageNumberByDate(Date date) {
    long day = 24*3600*1000;
    return  INIT_PAGE_NUMBER + (int) ((date.getTime() - mInitDate.getTime()) / day);
  }
  
  /*
   * Display specified date.
   */
  public void goToDate(Date date) {
    //Logger.error("EventPager.goToDate: go to page: " + getPageNumberByDate(date));
    setCurrentItem(getPageNumberByDate(date), false);
  }
  
  /*
   * Update pages when content has changed.
   */
  public void update() {
    mEventPagerAdapter.update();
    mEventPagerAdapter.notifyDataSetChanged();
  }
  
  private class EventPagerAdapter extends PagerAdapter{
  
    //save all events from database into adaptor
    private Vector<Event> events = new Vector<Event>();
    
    private TimetableDatabase db;
    
    public EventPagerAdapter(Date currentDate) {
      Logger.log("EventPagerAdapter created");
      db = TimetableDatabase.getInstance(mActivity);
      loadEvents();
    }
    
    public void update() {
      loadEvents();
    }
    
    private void loadEvents() {
      events = db.getAllEvents();
    }
    
    @Override
    public int getCount() {
      return Integer.MAX_VALUE;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
      return view == ((View) object);
    }
    
    @Override
    public Object instantiateItem(View viewPager, int pageNumber) {
      Logger.verbose("EventPager: try instantiate page " + Integer.toString(pageNumber));
      Date currentDate = EventPager.this.getDateByPageNumber(pageNumber);
      
      LinearLayout externalLayout = new LinearLayout(mActivity);
      externalLayout.setOrientation(LinearLayout.HORIZONTAL);
      ScrollView scrollView = new ScrollView(mActivity);
      scrollView.setLayoutParams(new ScrollView.LayoutParams(ScrollView.LayoutParams.MATCH_PARENT, ScrollView.LayoutParams.MATCH_PARENT));
      scrollView.setFillViewport(true);
      
      LinearLayout internalLayout = new LinearLayout(mActivity); 
      internalLayout.setOrientation(LinearLayout.VERTICAL);
      internalLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
      
      boolean hasEventsToday = false;
      
      for (Event event: events) {
        if (!event.isToday(currentDate)) {
          continue;
        }
        
        EventView eventView = mEventViewProvider.getView(pageNumber);
        eventView.populate(event, currentDate);
        eventView.setEventViewObserver(mActivity);
        eventView.setScrollView(scrollView);
        internalLayout.addView(eventView);
        hasEventsToday = true;
      }
      if (!hasEventsToday) {
        TextView textView = new TextView(mActivity);
        textView.setPadding(0,60,0,0);
        textView.setGravity(Gravity.CENTER_HORIZONTAL);
        textView.setText(R.string.event_pager_no_events);
        internalLayout.addView(textView);
      }
      
      scrollView.addView(internalLayout,0);
      externalLayout.addView(scrollView,0);
      
      ((ViewPager) viewPager).addView(externalLayout,0);
      
      Logger.log("EventPagerAdapter created page # "+ pageNumber + " " + new SimpleDateFormat("dd.MM.yyy").format(currentDate.getTime()));
      //logger.log("Events added to layout: " + internalLayout.getChildCount());
      return externalLayout;
    }
    
    //peace of code, that gets update function work
    public int getItemPosition(Object object){
         return POSITION_NONE;
    }
    
    @Override
      public void destroyItem(View viewPager, int pageNumber, Object view) {
              Logger.verbose("EventPagerAdapter destroys page number " + pageNumber);
              ((ViewPager) viewPager).removeView((View) view);
              mEventViewProvider.releaseViews(pageNumber);
    }
  }

}




Java Source Code List

com.timetable.android.AlarmSoundPreference.java
com.timetable.android.BroadcastActions.java
com.timetable.android.DeviceMuteService.java
com.timetable.android.EventBroadcastSender.java
com.timetable.android.EventChecker.java
com.timetable.android.EventController.java
com.timetable.android.EventPager.java
com.timetable.android.EventPeriod.java
com.timetable.android.EventService.java
com.timetable.android.EventViewProvider.java
com.timetable.android.EventView.java
com.timetable.android.Event.java
com.timetable.android.IllegalEventDataException.java
com.timetable.android.Logger.java
com.timetable.android.ServiceStarter.java
com.timetable.android.TimetableApp.java
com.timetable.android.TimetableDatabase.java
com.timetable.android.activities.EventAddActivity.java
com.timetable.android.activities.EventCopyActivity.java
com.timetable.android.activities.EventDayViewActivity.java
com.timetable.android.activities.EventEditActivity.java
com.timetable.android.activities.SettingsActivity.java
com.timetable.android.alarm.AlarmDialogActivity.java
com.timetable.android.alarm.AlarmService.java
com.timetable.android.alarm.EventAlarm.java
com.timetable.android.uitests.AlarmDialogActivityTestCase.java
com.timetable.android.uitests.EventAddActivityTestCase.java
com.timetable.android.uitests.TimetableUiTestCase.java
com.timetable.android.utils.DateFormatFactory.java
com.timetable.android.utils.DateUtils.java
com.timetable.android.utils.FakeTimeProvider.java
com.timetable.android.utils.SimpleTimeProvider.java
com.timetable.android.utils.TestAlarmStarter.java
com.timetable.android.utils.TimeProvider.java
com.timetable.android.utils.Utils.java