2011-08-26 52 views
1

我目前正在開發日曆應用。當用戶選擇日視圖,他需要得到類似這樣的例子的圖:Android:日曆中重疊事件的佈局

http://postimage.org/image/2sboxa22s/

我真的不知道如何做到這一點的佈局必須在運行時創建,因爲數量事件,持續時間,有多少重疊等是可變的。

我試圖找到沒有成功的類似問題。

我自己的兩分錢就像使用一個帶有LinearLayout的FrameLayout作爲背景(帶灰色的黃色線)和另一個放置事件的FrameLayout。唯一的問題是我不知道如何獲得安置權,也不知道如何處理事件重疊的地方。

UPDATE

由於是在Android的框架行跨度沒有這樣的事情(這是根本沒有任何佈局或插件支持),我不得不拿出自己的解決方案。

在短期方面,我做了以下內容:

  1. 添加間隔(空日曆),直到我到了第一次約會的開始。
  2. 將第一個約會添加到列表中,然後將與列表中的任意約會重疊的所有約會添加到列表中。
  3. 當沒有更多重疊的約會時,可以將該列表添加到日曆中。
  4. 我再次運行約會,以確定哪些可以在同一列,哪些不能。
  5. 添加填充空白布局的約會之間的空白空間所需的列數。
  6. 重複,直到一天的所有約會被添加。

該解決方案建立在一件事上:固定最小高度。這意味着我有一個預約必須有的最低高度。在我的情況下是60dp。然後,我找到最短的約會來找出一個因素,我可以計算出每個約會的高度。例如。如果最短的約會我15分鐘的因素將是4.

這種方式預約在15分鐘的短期約會,15分鐘長約60dp和1小時長約240dp的任命。

我不認爲這是一個最佳解決方案,因爲它不是100%動態的,但它完成了迄今爲止完成的工作。

+0

我不明白爲什麼每個人都要重新發明輪子。 當然,大多數項目並不是真正的開源,但這可能是第1000個日曆應用程序... – ppuschmann

+0

即使這是日曆應用程序nr。 1000 - 可用的不符合我正在開發的公司的要求。因此我需要自己構建整個應用程序。這個問題同樣也是一個關於如何實現真正的rowspan的問題,因爲這個功能在android框架中是不可用的。 – kaspermoerch

回答

2

我深深地看着你的問題,但是我發現有與您的佈局設計幾個問題,必須產生通過識別觀點一類事件並相應地把它們不重疊任何其他事件

http://postimg.org/image/4lqmlnhk7/(輸出爲參考)

private int[] Gettimespan(int SHour, int EHour, int SMinutes, int EMinutes, int TotalWidth) { 
    // float x = (60/Minutes) * 100; 
    int x = 0; // width for time marking 
    int y = 0; // Height For hour marking 
    int z = 0; // Margin left for Stime 
    int YAxis = 0; // YAxis Adjustment 

    int TotalYAxis = 80; // /Total YAxis of Per hour slot 
    int HalfOf_TotalYAxis = 40; // /Half of the Total YAxis of Per hour slot 
    int MinutesDiff = GetMinutes(SHour, EHour, SMinutes, EMinutes); 



    int xAxisFinetuning = 0; 
    //Check If any Views Between 
    final RelativeLayout rl = (RelativeLayout) _activity.findViewById(R.id.relativeLayout4); 
    List<Integer> ViewCount = DetectView(SHour, rl); 
    if(ViewCount.size() > 1 && ViewCount != null) 
    { 
     for(int i = 0; i < ViewCount.size(); i++) 
     { 
      LinearLayout ll = (LinearLayout) _activity.findViewById(ViewCount.get(i)); 
      int Stepper = TotalWidth/ViewCount.size(); 
      xAxisFinetuning += Stepper; 
      z = xAxisFinetuning; 
      ll.setMinimumWidth(Stepper - 15); 
      ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) ll.getLayoutParams(); 
      p.leftMargin = z; 
      ll.requestLayout(); 
      x = Stepper - 26; 
      //Log.d("LayoutID", "" + ll.getId()); 
     } 
    } 

    //Snippet 1: Case Same hour but upper span 
    //if(SMinutes >= 0 && EMinutes >= 30 && SMinutes <= 30 && ((SHour+1 == EHour) || (SHour == EHour))) 
    if(SMinutes >= 0 && EMinutes >= 30 && SMinutes <= 30 && (SHour == EHour)) 
    { 
     if(x == 0) 
     { 
      x = TotalWidth; 
     } 

     if(y == 0) 
     { 
      if(MinutesDiff > 30) 
      { 
       y = dpToPx(TotalYAxis); 
      } 
      else 
      { 
       y = dpToPx(HalfOf_TotalYAxis); 
      } 
     } 
     //if(z == 0) 
     //{ 
     z = 0; 
     //} 
     if(MinutesDiff >= 30) 
     { 
      YAxis = 0; 
     } 
     else 
     { 
      YAxis = HalfOf_TotalYAxis; 
     } 

     return new int[] { x, y - 6, z, YAxis }; 
    } 

    // End Snippet 1. 

    //Snippet 2: Case Same hour but lower span 
    //if(SMinutes >= 30 && ((SHour+1 == EHour) || (SHour == EHour))) 
    if(SMinutes >= 30 && (SHour == EHour)) 
    { 
     if(x == 0) 
     { 
      x = TotalWidth; 
     } 

     if(y == 0) 
     { 
      if(MinutesDiff > 30) 
      { 
       y = dpToPx(TotalYAxis); 
      } 
      else 
      { 
       y = dpToPx(HalfOf_TotalYAxis); 
      } 
     } 

     z = 0; 

     if(MinutesDiff > 30) 
     { 
      YAxis = 0; 
     } 
     else 
     { 
      YAxis = HalfOf_TotalYAxis; 
     } 


     return new int[] { x, y - 6, z, YAxis }; 
    } 

    // End Snippet 2. 

    //Snippet 3: Case when Total Hour > 1 

    if(SHour != EHour) 
    { 
     if(x == 0) 
     { 
     x = TotalWidth; 
     } 
     if(y == 0) 
     { 
      Log.d("Calendar Case Next Day", "Ehour " + EHour + " Shour " + SHour); 
      if((EHour - SHour)<=1) 
      { 
      y = dpToPx(((EHour - SHour)+1) * TotalYAxis); 
      } 
      else 
      { 
       y = dpToPx(((EHour - SHour)) * TotalYAxis); 
      } 
     } 
     z = 0; 
     YAxis = 0; 

     return new int[] { x, y - 6, z, YAxis }; 
    } 

    // End Snippet 3. 


    return new int[] { x, y - 6, z, YAxis }; 
    } 

private void TimeSpanning(LinearLayout LL, int SHour, int EHour, int SMinutes, int EMinutes, int TotalWidth, TextView Tv) { 

    if(SHour > EHour) 
    { 
     Log.d("Calendar Case Next Day_", "Ehour " + EHour + " Shour " + SHour + " TextView " + Tv.getText()); 
     EHour = 24; 
     //EMinutes = 59; 
    } 

    int minuteAdjustment = 0; 

    if((EHour + ":" + EMinutes).equalsIgnoreCase(SHour + ":" + SMinutes)) 
    { 
     //Log.d("EqualFound","True"); 
     minuteAdjustment = 59; 
    } 

    int[] x = Gettimespan(SHour, EHour, SMinutes, EMinutes + minuteAdjustment, TotalWidth); 

    //Log.d("Eminute"," - " + EMinutes); 

    LL.setMinimumWidth(x[0]); 
    LL.setMinimumHeight(x[1]); 

    Tv.setWidth(x[0]); 
    Tv.setHeight(x[1] - 14); 

    int Padding_Top_Bottom = 4; 
    int Padding_Left_Right = 4; 
    //LL.setPadding(30, 30, 30, 30); 
    //tv.setWidth(x[0]); 
    //tv.setHeight(x[1]); 

    ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) LL.getLayoutParams(); 
    //ViewGroup.MarginLayoutParams p1 = (ViewGroup.MarginLayoutParams) ll.getLayoutParams(); 

    switch (SHour) { 
    case 0: { 
     // 12 am 
     p.setMargins(x[2] + Padding_Left_Right, dpToPx(GetCalendarScaleY(0) + x[3]) + Padding_Top_Bottom, Padding_Left_Right, 0); 
     LL.requestLayout(); 
     break; 
    } 

    case 1: { 
     // 1 am 
     p.setMargins(x[2] + Padding_Left_Right, dpToPx(GetCalendarScaleY(1) + x[3]) + Padding_Top_Bottom, Padding_Left_Right, 0); 
     LL.requestLayout(); 
     break; 
    } 

    case 2: { 
     // 2 am 
     p.setMargins(x[2] + Padding_Left_Right, dpToPx(GetCalendarScaleY(2) + x[3]) + Padding_Top_Bottom, Padding_Left_Right, 0); 
     LL.requestLayout(); 
     break; 
    } 

    case 3: { 
     // 3 am 
     p.setMargins(x[2] + Padding_Left_Right, dpToPx(GetCalendarScaleY(3) + x[3]) + Padding_Top_Bottom, Padding_Left_Right, 0); 
     LL.requestLayout(); 
     break; 
    } 

    case 4: { 
     // 4 am 
     p.setMargins(x[2] + Padding_Left_Right, dpToPx(GetCalendarScaleY(4) + x[3]) + Padding_Top_Bottom, Padding_Left_Right, 0); 
     LL.requestLayout(); 
     break; 
    } 

    case 5: { 
     // 5 am 
     p.setMargins(x[2] + Padding_Left_Right, dpToPx(GetCalendarScaleY(5) + x[3]) + Padding_Top_Bottom, Padding_Left_Right, 0); 
     LL.requestLayout(); 
     break; 
    } 

    case 6: { 
     // 6 am 
     p.setMargins(x[2] + Padding_Left_Right, dpToPx(GetCalendarScaleY(6) + x[3]) + Padding_Top_Bottom, Padding_Left_Right, 0); 
     LL.requestLayout(); 
     break; 
    } 

    case 7: { 
     // 7 am 
     p.setMargins(x[2] + Padding_Left_Right, dpToPx(GetCalendarScaleY(7) + x[3]) + Padding_Top_Bottom, Padding_Left_Right, 0); 
     LL.requestLayout(); 
     break; 
    } 

    case 8: { 
     // 8 am 
     p.setMargins(x[2] + Padding_Left_Right, dpToPx(GetCalendarScaleY(8) + x[3]) + Padding_Top_Bottom, Padding_Left_Right, 0); 
     LL.requestLayout(); 
     break; 
    } 

    case 9: { 
     // 9 am 
     p.setMargins(x[2] + Padding_Left_Right, dpToPx(GetCalendarScaleY(9) + x[3]) + Padding_Top_Bottom, Padding_Left_Right, 0); 
     LL.requestLayout(); 
     break; 
    } 

    case 10: { 
     // 10 am 
     p.setMargins(x[2] + Padding_Left_Right, dpToPx(GetCalendarScaleY(10) + x[3]) + Padding_Top_Bottom, Padding_Left_Right, 0); 
     LL.requestLayout(); 
     break; 
    } 

    case 11: { 
     // 11 am 
     p.setMargins(x[2] + Padding_Left_Right, dpToPx(GetCalendarScaleY(11) + x[3]) + Padding_Top_Bottom, Padding_Left_Right, 0); 
     LL.requestLayout(); 
     break; 
    } 

    case 12: { 
     // 12 pm 
     p.setMargins(x[2] + Padding_Left_Right, dpToPx(GetCalendarScaleY(12) + x[3]) + Padding_Top_Bottom, Padding_Left_Right, 0); 
     LL.requestLayout(); 
     break; 
    } 

    case 13: { 
     // 1 pm 
     p.setMargins(x[2] + Padding_Left_Right, dpToPx(GetCalendarScaleY(13) + x[3]) + Padding_Top_Bottom, Padding_Left_Right, 0); 
     LL.requestLayout(); 
     break; 
    } 

    case 14: { 
     // 2 pm 
     p.setMargins(x[2] + Padding_Left_Right, dpToPx(GetCalendarScaleY(14) + x[3]) + Padding_Top_Bottom, Padding_Left_Right, 0); 
     LL.requestLayout(); 
     break; 
    } 

    case 15: { 
     // 3 pm 
     p.setMargins(x[2] + Padding_Left_Right, dpToPx(GetCalendarScaleY(15) + x[3]) + Padding_Top_Bottom, Padding_Left_Right, 0); 
     LL.requestLayout(); 
     break; 
    } 

    case 16: { 
     // 4 pm 
     p.setMargins(x[2] + Padding_Left_Right, dpToPx(GetCalendarScaleY(16) + x[3]) + Padding_Top_Bottom, Padding_Left_Right, 0); 
     LL.requestLayout(); 
     break; 
    } 

    case 17: { 
     // 5 pm 
     p.setMargins(x[2] + Padding_Left_Right, dpToPx(GetCalendarScaleY(17) + x[3]) + Padding_Top_Bottom, Padding_Left_Right, 0); 
     LL.requestLayout(); 
     break; 
    } 

    case 18: { 
     // 6 pm 
     p.setMargins(x[2] + Padding_Left_Right, dpToPx(GetCalendarScaleY(18) + x[3]) + Padding_Top_Bottom, Padding_Left_Right, 0); 
     LL.requestLayout(); 
     break; 

    } 

    case 19: { 
     // 7 pm 
     p.setMargins(x[2] + Padding_Left_Right, dpToPx(GetCalendarScaleY(19) + x[3]) + Padding_Top_Bottom, Padding_Left_Right, 0); 
     LL.requestLayout(); 
     break; 
    } 

    case 20: { 
     // 8 pm 
     p.setMargins(x[2] + Padding_Left_Right, dpToPx(GetCalendarScaleY(20) + x[3]) + Padding_Top_Bottom, Padding_Left_Right, 0); 
     LL.requestLayout(); 
     break; 
    } 

    case 21: { 
     // 9 pm 
     p.setMargins(x[2] + Padding_Left_Right, dpToPx(GetCalendarScaleY(21) + x[3]) + Padding_Top_Bottom, Padding_Left_Right, 0); 
     LL.requestLayout(); 
     break; 
    } 

    case 22: { 
     // 10 pm 
     p.setMargins(x[2] + Padding_Left_Right, dpToPx(GetCalendarScaleY(22) + x[3]) + Padding_Top_Bottom, Padding_Left_Right, 0); 
     LL.requestLayout(); 
     break; 
    } 
    case 23: { 
     // 11 pm 
     p.setMargins(x[2] + Padding_Left_Right, dpToPx(GetCalendarScaleY(23) + x[3]) + Padding_Top_Bottom, Padding_Left_Right, 0); 
     LL.requestLayout(); 
     break; 
    } 

    } 

} 

private int dpToPx(int dp) { 
    if(_activity != null) 
    { 
      DisplayMetrics displayMetrics = _activity.getResources().getDisplayMetrics(); 
      int px = 0; 
      if(displayMetrics != null) 
      { 
       px = Math.round((float) dp * ((float)displayMetrics.densityDpi/(float)DisplayMetrics.DENSITY_DEFAULT)); 

      return px; 
      } 
    } 
      return 0; 
     } 

private int GetCalendarScaleY(int Time) { 

    //Coordinates of Rows (Y Axis with respect to hours) 
    int x = 0; 

    switch (Time) { 
    case 0: { 
     // 12 am 
     x = 0; 
     break; 
    } 

    case 1: { 
     // 1 am 
     x = 80; 
     break; 
    } 

    case 2: { 
     // 2 am 
     x = 160; 
     break; 
    } 

    case 3: { 
     // 3 am 
     x = 240; 
     break; 
    } 

    case 4: { 
     // 4 am 
     x = 320; 
     break; 
    } 

    case 5: { 
     // 5 am 
     x = 400; 
     break; 
    } 

    case 6: { 
     // 6 am 
     x = 480; 
     break; 
    } 

    case 7: { 
     // 7 am 
     x = 560; 
     break; 
    } 

    case 8: { 
     // 8 am 
     x = 640; 
     break; 
    } 

    case 9: { 
     // 9 am 
     x = 720; 
     break; 
    } 

    case 10: { 
     // 10 am 
     x = 800; 
     break; 
    } 

    case 11: { 
     // 11 am 
     x = 880; 
     break; 
    } 

    case 12: { 
     // 12 pm 
     x = 960; 
     break; 
    } 

    case 13: { 
     // 1 pm 
     x = 1040; 
     break; 
    } 

    case 14: { 
     // 2 pm 
     x = 1120; 
     break; 
    } 

    case 15: { 
     // 3 pm 
     x = 1200; 
     break; 
    } 

    case 16: { 
     // 4 pm 
     x = 1280; 
     break; 
    } 

    case 17: { 
     // 5 pm 
     x = 1360; 
     break; 
    } 

    case 18: { 
     // 6 pm 
     x = 1440; 
     break; 
    } 

    case 19: { 
     // 7 pm 
     x = 1520; 
     break; 
    } 

    case 20: { 
     // 8 pm 
     x = 1600; 
     break; 
    } 

    case 21: { 
     // 9 pm 
     x = 1680; 
     break; 
    } 

    case 22: { 
     // 10 pm 
     x = 1760; 
     break; 
    } 
    case 23: { 
     // 11 pm 
     x = 1840; 
     break; 
    } 

    } 

    return x; 
}