1

私は現在、カレンダーアプリに取り組んでいます。ユーザーが日ビューを選択すると、次の例のようなビューを取得する必要があります。

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

イベントの数、その期間、重複する数などは可変であるため、実行時にレイアウトを作成する必要があるため、これを行う方法がよくわかりません。

同様の問題を見つけようとしましたが、成功しませんでした。

私自身の 2 セントは、背景 (黄色の灰色の線) に LinearLayout を持つ FrameLayout を使用し、イベントが配置されるその上に別の FrameLayout を使用するようなものです。これに関する唯一の問題は、配置を正しくする方法や、イベントが重なる場所を処理する方法がわからないことです。

アップデート

Android フレームワークには rowspan などがないため (どのレイアウトやウィジェットでもサポートされていないため)、独自のソリューションを考え出す必要がありました。

簡単に言えば、私は次のことを行います。

  1. 最初の予定の開始時刻に達するまで、スペーサー (空のカレンダー) を追加します。
  2. 最初の予定をリストに追加してから、リスト内のいずれかの予定と重複するすべての予定をリストに追加します。
  3. 重複する予定がなくなったら、リストをカレンダーに追加します。
  4. もう一度アポイントメントを調べて、同じ列に配置できるものとできないものを判断します。
  5. 空のレイアウトで予定の間の空きスペースを埋める必要な数の列を追加します。
  6. その日の予定がすべて追加されるまで繰り返します。

このソリューションは、固定された最小高さという 1 つのことに基づいています。これは、予定に必要な最低限の身長があることを意味します。私の場合は60dpです。次に、最短の予定を見つけて、各予定の高さを計算できる要因を見つけます。たとえば、最短の予定が 15 分である場合、係数は 4 になります。

このように、最短の予定が 15 分の列の予定、15 分の長い予定は 60 dp、1 時間の予定は 240 dp になります。

これは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 に答える