English 中文(简体)
安德列斯:日历年度重叠事件说明
原标题:Android: Layout for overlapping events in calendar

I m目前正在编制一个日历表。 在用户选择“日观”时,他需要看到类似的例子:

http://postimage.org/image/2s Boxa22s/

I m not really sure how to do this as the layout must be created at runtime because the number of events, their duration, how many are overlapping etc. is variable.

我试图找到类似的问题,但却没有成功。

我的两分之四,像利用带线的“支线”的“支离破碎”来形容背景(有灰色线的黄色线)和把事件放在首位的另一种碎片。 只有这样,我才不敢肯定如何获得安置权,也不知道如何处理事件重叠的地方。

<>>>>>

既然不存在像安乐团这样的一刀(它根本得不到任何布局或植被的支持) 我必须拿出自己的解决办法。

简言之,我做了以下工作:

  1. Add a spacer (empty calendar) until I reach the start of the first appointment.
  2. Add first appointment to a list and then add all appointments that overlaps any one appointment in the list to the list.
  3. When there are no more overlapping appointments its time to add the list to the calendar.
  4. I run through the appointments again to determine which can be in the same column and which cannot.
  5. Add the required number of columns filling empty space between appointments with empty layouts.
  6. Repeat untill all appointments of the day are added.

这一解决办法基于以下一点: 固定最低高度。 这意味着,我至少要高一个任命。 在我的案件中,第60页。 然后,我发现任命时间最短,以图示我能够计算每项任命高峰的系数。 例如,如果任命时间最长为15分钟,系数为4。

这样一来,短期任用为15分钟,任期为15分钟,长期任用为60分钟,长期任用为240分钟。

我不认为这是一个最佳的解决办法,因为它不是100%的生机,而是迄今为止取得了工作。

问题回答

然而,我对你的问题进行了深思熟虑,发现你的布局设计没有什么问题,你必须通过确定看法的班子来开展各种活动,从而避免任何其它事件的重叠。

http://postimg.org/image/4lqmlnhk7/ (Output for Reference)

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;
}




相关问题
Java Checking number months

I think I figure out what question I am trying to ask I dont want get(Calendar.MONTH) I want the month not to be greater than the last caldendar month that is why i am doing month -1, i realize that ...

How to show custom events to a Kronolith calendar

I want to show events from an external source in the Kronolith calendar views. I don t want to do this by exporting the events to iCal and then showing them as an external calendar. The events are ...

How to reset a field when changing a date ui.datepicker.js

I m using ui.datepicker.js I have a form and when the date is changed I want that another field reset the status (this field is a dropdown select box). The case is the following: The user enter the ...

Calendar returns wrong month [duplicate]

Calendar rightNow = Calendar.getInstance(); String month = String.valueOf(rightNow.get(Calendar.MONTH)); After the execution of the above snippet, month gets a value of 10 instead of 11. How come?

How to determine if date/time is EDT or EST in Java?

I have a PDF that users must complete. The PDF has date and time fields on it. The PDF instructs the users to input the date and time in Eastern format (Not EST or EDT). When the user completes the ...

热门标签