How to do Planner/ business Calendar in android

2019-08-30 12:05发布

问题:

I am planning to do calendar which contain dynamic event below date. But i am not getting good sample regarding it. What is exact name call as per image below in android or share some idea. I got many apk and image related to it. but no exact idea behind it, does android support it. I plan is to show calendar in Month, week, day wise. After searching lot in google I am writing this. Does it supports all version from 2.3 to 4. i have seen some blog business calendar supports on above verison 4. Thanks a lot for kind and great ideas.

回答1:

Try to implement with GridView, below code for Gridview Adapter :

public class GridCellAdapter extends BaseAdapter //implements OnClickListener 
 {
  private final Context _context;

  private final List list;
  private static final int DAY_OFFSET = 1;
  private final String[] months = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept", "Oct","Nov", "Dec" };
  private final int[] daysOfMonth = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  private int daysInMonth;
  private int currentDayOfMonth;
  private int currentWeekDay;
  Calendar calendar;
  private Button gridcell;

  List objects;

  // Days in Current Month
  public GridCellAdapter(Context context, int textViewResourceId, int month, int year, List objects) 
  {
   super();
   this._context = context;
   this.list = new ArrayList();
   this.objects=objects;
   Calendar calendar = Calendar.getInstance();
   setCurrentDayOfMonth(calendar.get(Calendar.DAY_OF_MONTH));
   setCurrentWeekDay(calendar.get(Calendar.DAY_OF_WEEK));

   // Print Month
   printMonth(month, year);
  }

  private String getMonthAsString(int i) 
  {
   return months[i];
  }

  private int getNumberOfDaysOfMonth(int i) 
  {
   return daysOfMonth[i];
  }

  public String getItem(int position) 
  {
   return list.get(position);
  }

  @Override
  public int getCount()
  {
   return list.size();
  }

  /**
   * Prints Month
   * 
   * @param mm
   * @param yy
   */
  private void printMonth(int mm, int yy) 
  {
   int trailingSpaces = 0;
   int daysInPrevMonth = 0;
   int prevMonth = 0;
   int prevYear = 0;
   int nextMonth = 0;
   int nextYear = 0;

   int currentMonth = mm - 1;
   getMonthAsString(currentMonth);
   daysInMonth = getNumberOfDaysOfMonth(currentMonth);

   GregorianCalendar cal = new GregorianCalendar(yy, currentMonth, 1);
   if (currentMonth == 11) 
   {
    prevMonth = currentMonth - 1;
    daysInPrevMonth = getNumberOfDaysOfMonth(prevMonth);
    nextMonth = 0;
    prevYear = yy;
    nextYear = yy + 1;
   }
   else if (currentMonth == 0) 
   {
    prevMonth = 11;
    prevYear = yy - 1;
    nextYear = yy;
    daysInPrevMonth = getNumberOfDaysOfMonth(prevMonth);
    nextMonth = 1;
   } 
   else 
   {
    prevMonth = currentMonth - 1;
    nextMonth = currentMonth + 1;
    nextYear = yy;
    prevYear = yy;
    daysInPrevMonth = getNumberOfDaysOfMonth(prevMonth);
   }

   int currentWeekDay = cal.get(Calendar.DAY_OF_WEEK) - 1;
   trailingSpaces = currentWeekDay;

   if (cal.isLeapYear(cal.get(Calendar.YEAR)))
   {
    if (mm == 2)
    {
     ++daysInMonth;
    }
    else if (mm == 3)
    {
     ++daysInPrevMonth;
    }
   }

   for (int i = 0; i < trailingSpaces; i++) 
   {
    list.add(String.valueOf((daysInPrevMonth - trailingSpaces + DAY_OFFSET) + i) + "-GREY" + "-" + getMonthAsString(prevMonth)+ "-" + prevYear+"-no");
   }

   // Current Month Days
   for (int i = 1; i <= daysInMonth; i++)
   {
    if (i == getCurrentDayOfMonth()) 
    {
     list.add(String.valueOf(i) + "-BLUE" + "-" + getMonthAsString(currentMonth) + "-" + yy+"-yes");
    }
    else 
    {
     list.add(String.valueOf(i) + "-WHITE" + "-" + getMonthAsString(currentMonth) + "-" + yy+"-no");
    }
   }

   // Leading Month days
   for (int i = 0; i < list.size() % 7; i++) 
   {
    list.add(String.valueOf(i + 1) + "-GREY" + "-" + getMonthAsString(nextMonth) + "-" + nextYear+"-no");
   }
  }

  @Override
  public long getItemId(int position) 
  {
   return position;
  }

  @Override
  public View getView(final int position, View convertView, ViewGroup parent)
  {
   View row = convertView;
   if (row == null) 
   {
    LayoutInflater inflater = (LayoutInflater) _context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    row = inflater.inflate(R.layout.screen_gridcell, parent, false);
   }

   gridcell = (Button) row.findViewById(R.id.calendar_day_gridcell);

   String[] day_color = list.get(position).split("-");
   String theday = day_color[0];
   gridcell.setText(theday.trim());
   gridcell.setTag("");

   Calendar calendar = Calendar.getInstance();
      calendar.set(Integer.parseInt(day_color[3].trim()), convertMonthToInt(day_color[2].trim())-1, Integer.parseInt(day_color[0].trim()));
       int val = calendar.get(Calendar.DAY_OF_WEEK);


   gridcell.setGravity(Gravity.CENTER);

   gridcell.setBackgroundResource(R.drawable.cal_box_grey);

   if (day_color[1].equals("GREY")) 
   {
    gridcell.setBackgroundResource(R.drawable.cal_box_grey);
    gridcell.setTextColor(getResources().getColor(R.color.gray));
   }
   if (day_color[1].equals("WHITE")) 
   {
    gridcell.setBackgroundResource(R.drawable.cal_box);    
    gridcell.setTextColor(getResources().getColor(R.color.purple_dark));
    if(val == 1 || val == 7)
        {
         gridcell.setBackgroundResource(R.drawable.circle_grey_border);    
         gridcell.setTextColor(getResources().getColor(R.color.lightgray02));
        }
   }

   for (int i = 0; i < objects.size(); i++) 
   {
    if(Integer.parseInt(theday)==Integer.parseInt((objects.get(i).day))
      && day_color[2].equals(objects.get(i).month))
    {
     gridcell.setTag(objects.get(i).title+":::"+objects.get(i).address+":::"+objects.get(i).image
       +":::"+objects.get(i).day+":::"+objects.get(i).month+":::"+objects.get(i).year);
     gridcell.setTextColor(Color.parseColor("#da0078"));
    }
   }

   if (day_color[1].equals("BLUE")&& day_color[4].equals("yes"))
   {
    gridcell.setTextColor(Color.parseColor("#ffffff"));
    gridcell.setBackgroundResource(R.drawable.circle_days_fill);
   }

   if (day_color[4].equals("yes"))
   {
    gridcell.setTextColor(Color.parseColor("#ffffff"));
    gridcell.setBackgroundResource(R.drawable.circle_days_fill);
   }

   if (day_color[1].equals("BLUE")&& day_color[4].equals("no"))
   {
    gridcell.setTextColor(Color.parseColor("#ffffff"));
    gridcell.setBackgroundResource(R.drawable.circle_days_fill);
   }



   for (int i = 0; i < objects.size(); i++) 
   {
    if(Integer.parseInt(theday)==Integer.parseInt((objects.get(i).day))
      && day_color[2].equals(objects.get(i).month))
    {
     if(objects.get(i).rsvp.equals("1"))
     {
      gridcell.setTextColor(Color.parseColor("#008000"));
     }
    }
   }

   gridcell.setOnClickListener(new OnClickListener()
   {
    @Override
    public void onClick(View v) 
    {
     // TODO Auto-generated method stub
     String tag [] = list.get(position).toString().split("-");
     if(v.getTag().toString().equals("1"))
     {
      v.setTag("1");
      if(v.getId() == R.id.calendar_day_gridcell)
      {
       if (myToolTipInfoWindow == null)
       {
        addPurpleToolTipView(v);
       }
       else
       {
        myToolTipInfoWindow.remove();
        myToolTipInfoWindow = null;
       }
      }
      else
      {
      }
     }
     else if(v.getTag().toString().equals(""))
     {
      v.setTag("1");
      if(v.getId() == R.id.calendar_day_gridcell)
      {
       if (myToolTipInfoWindow == null)
       {
        addPurpleToolTipView(v);
       }
       else
       {
        myToolTipInfoWindow.remove();
        myToolTipInfoWindow = null;
       }
      }
      else
      {
      }
     }
     else
     {
      if(v.getId() == R.id.calendar_day_gridcell)
      {
       if (myToolTipInfoWindow == null)
       {
        addPurpleToolTipView(v);
       }
       else
       {
        myToolTipInfoWindow.remove();
        myToolTipInfoWindow = null;
       }
      }
      else
      {
      }
     }
     for (int i = 0; i < list.size(); i++) 
     {
      gridcell.setTextColor(R.color.purple_lite);
      gridcell.setBackgroundColor(Color.parseColor("#da0078"));
      String tags [] = list.get(i).toString().split("-");
      list.set(i,tags[0]+"-"+tags[1]+"-"+tags[2]+"-"+tags[3]+"-no");
     }
     list.set(position,tag[0]+"-"+tag[1]+"-"+tag[2]+"-"+tag[3]+"-yes");
     adapter1.notifyDataSetChanged();

    }
   });

   return row;
  }

  public int getCurrentDayOfMonth() 
  {
   return currentDayOfMonth;
  }

  private void setCurrentDayOfMonth(int currentDayOfMonth)
  {
   this.currentDayOfMonth = currentDayOfMonth;
  }

  public void setCurrentWeekDay(int currentWeekDay) 
  {
   this.currentWeekDay = currentWeekDay;
  }

  public int getCurrentWeekDay() 
  {
   return currentWeekDay;
  }
 }

screen_gridcell.xml code :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="60dp"
    android:layout_height="50dp"
    android:background="#ffffff"
    android:orientation="vertical" >

    <Button
        android:id="@+id/calendar_day_gridcell"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:button="@drawable/calendar_button_selector"
        android:gravity="center"
        android:textSize="13sp"
        android:textStyle="bold"
        android:textColor="#FFFFFF" >
    </Button>

</RelativeLayout>

Screenshot