Android Open Source - BonetCalendarView Grid Bt Month View Provider






From Project

Back to project page BonetCalendarView.

License

The source code is released under:

Apache License

If you think the Android project BonetCalendarView 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

/* Copyright 2014 Eduardo Bonet
/*from w  w  w  . j  a v a2 s . c o m*/
   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License. */

package com.bonet.views;

import com.bonet.views.bonetcalendarview.R;

import android.content.Context;
import android.util.Log;
import android.util.MonthDisplayHelper;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.AdapterView.OnItemClickListener;

/**
 * A implementation of the BtMonthViewProvider that will display days in a grid fashion,
 * just like a regular calendar
 * 
 * @author Eduardo Bonet
 *
 */
public class GridBtMonthViewProvider extends BtMonthViewProvider {

  // The adapter
  private DayGridAdapter mAdapter;
  
  // the grid view
  private GridView mGridView;
  
  // the on item click listener
  private OnItemClickListener mGridItemClickedListener;
  
  // the context
  private Context mContext;
  
  /* VERY handy class that will help us display the month as a grid */
  private MonthDisplayHelper mMonthDisplay;
  
  public GridBtMonthViewProvider(Context context, BtMonth month) {
    super(month);
    
    mContext = context;
    
    Log.d("", "Criando DayGridAdapter");
    mAdapter = new DayGridAdapter(context,month,getMinDate(),getMaxDate());

    mMonthDisplay = new MonthDisplayHelper(month.getYear(), month.getMonth());
  
    mGridItemClickedListener = new OnItemClickListener() {
      
      @Override
      public void onItemClick(AdapterView<?> adapter, View arg1, int position,
          long arg3) {
        
        int fp = mMonthDisplay.getRowOf(1)*7+mMonthDisplay.getColumnOf(1);
        int lp = fp+ mMonthDisplay.getNumberOfDaysInMonth();
        
        // Checks to see if the position selected represents a date within the month
        if(position >= fp && position <lp) {
        
          BtDate day;
          // Checks to see if the selected date is within the defined bounds
          if( (day = getMonth().getDate(position+1-fp)).isWithinBounds(getMinDate(),getMaxDate()))
            selectDay(day);
          
        }
      }
    };
    
  }
  
  @Override
  public void setMonth(BtMonth month) {
    mMonthDisplay = new MonthDisplayHelper(month.getYear(), month.getMonth());
    
    mAdapter.setMonth(month);
    
    super.setMonth(month);
  }

  @Override
  public View getView() {
    
    if(mGridView == null) {
      
      // Creates the grid view
      mGridView = (GridView)LayoutInflater.from(mContext).inflate(R.layout.grid_view, null);
      
      // 7 days in the week
      mGridView.setNumColumns(7);
      
      // Sets the adapter
      mGridView.setAdapter(mAdapter);
      
      // sets the on click listener
      mGridView.setOnItemClickListener(mGridItemClickedListener);
      
    }
    
    return mGridView;
  }

  @Override
  public void updateView() {

    if(null != mAdapter)
      mAdapter.notifyDataSetChanged();
  }
  
  /**
   * Sets the adapter for the grid view
   * @param adapter
   */
  public void setAdapter(DayGridAdapter adapter) {
    mAdapter = adapter;
    if(mGridView != null)
      mGridView.setAdapter(adapter);
  }

  @Override
  public void setMinDate(BtDate date) {
    super.setMinDate(date);
    mAdapter.setMinDate(date);
  }
  
  @Override
  public void setMaxDate(BtDate date) {
    super.setMaxDate(date);
    mAdapter.setMaxDate(date);
  }
  
  /**
   * @return the context 
   */
  public Context getContext(){
    return mContext;
  }
  
  /**
   * @return the grid view that displays the days
   */
  public GridView getGridView(){
    return mGridView;
  }
  
  /**
   * @param grid that displays the will
   */
  public void setGridView(GridView grid){
    mGridView = grid;
    updateView();
  }
  
  /**
   * @return the default on grid view on item click listener
   */
  public OnItemClickListener getDefaultItemClickListener(){
    return mGridItemClickedListener;
  }
  
  /**
   * @return the current data adapter
   */
  public DayGridAdapter getAdapter(){
    return mAdapter;
  }
  
}




Java Source Code List

com.bonet.example.bonetcalendarviewactivity.CustomDayGridAdapter.java
com.bonet.example.bonetcalendarviewactivity.CustomGridCalendarActivity.java
com.bonet.example.bonetcalendarviewactivity.CustomGridCalendar.java
com.bonet.example.bonetcalendarviewactivity.CustomMonthProvider.java
com.bonet.example.bonetcalendarviewactivity.CustomYearAdapter.java
com.bonet.example.bonetcalendarviewactivity.CustomYearProvider.java
com.bonet.example.bonetcalendarviewactivity.ExampleActivity.java
com.bonet.example.bonetcalendarviewactivity.GridCalendarActivity.java
com.bonet.example.bonetcalendarviewactivity.ListCalendarActivity.java
com.bonet.views.BtCalendarView.java
com.bonet.views.BtCalendar.java
com.bonet.views.BtDate.java
com.bonet.views.BtMonthViewProvider.java
com.bonet.views.BtMonth.java
com.bonet.views.BtYearViewProvider.java
com.bonet.views.DayGridAdapter.java
com.bonet.views.DayListAdapter.java
com.bonet.views.GridBtMonthViewProvider.java
com.bonet.views.ListBtMonthViewProvider.java
com.bonet.views.ListBtYearViewProvider.java
com.bonet.views.MonthListAdapter.java
com.bonet.views.OnDateSelectedListener.java
com.bonet.views.package-info.java