1

我目前正在开发一个日历应用程序。当用户选择 Day View 时,他需要得到一个类似于这个例子的视图:

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

我不确定如何执行此操作,因为必须在运行时创建布局,因为事件的数量、它们的持续时间、重叠的数量等是可变的。

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

我自己的两分钱就像使用 FrameLayout 和 LinearLayout 作为背景(黄色带灰线)和另一个 FrameLayout 在放置事件的顶部。唯一的问题是我不确定如何正确放置,也不知道如何处理事件重叠的地方。

更新

由于在 Android 框架中没有行跨度之类的东西(任何布局或小部件都不支持它),我不得不提出自己的解决方案。

简而言之,我会执行以下操作:

  1. 添加一个间隔(空日历),直到我到达第一个约会的开始。
  2. 将第一个约会添加到列表中,然后将与列表中任何一个约会重叠的所有约会添加到列表中。
  3. 当没有更多重叠的约会时,是时候将列表添加到日历中了。
  4. 我再次浏览约会以确定哪些可以在同一列中,哪些不能。
  5. 添加所需数量的列填充具有空布局的约会之间的空白空间。
  6. 重复直到添加了当天的所有约会。

该解决方案基于一件事:固定最小高度。这意味着我有一个约会必须有的最低身高。在我的情况下,它是 60dp。然后我找到最短的约会来找出一个因素,我可以从中计算出每个约会的高度。例如,如果最短的约会时间为 15 分钟,则系数为 4。

这样,短约会为 15 分钟的列中的约会,15 分钟长的约会将是 60dp,1 小时长的约会将是 240dp。

我不认为这是一个最佳解决方案,因为它不是 100% 动态的,但到目前为止它可以完成工作。

4

1 回答 1

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;
}
于 2015-01-29T08:13:48.557 回答