外一个datepicker的

外一个datepicker的

本文介绍了CalendarView问题的时候直接使用(外一个datepicker的)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 CalendarView 直接,而不是一个的DatePicker ,B / C它被用作降-down /弹出对话框风格在空间因子(用户点击位于日期字段右侧的按钮, CalendarView 滴下来,直接出现在以下领域对齐/锚定到的按钮)。

双显示停车问题我跑进,并且没有决心花了一个完整的10小时工作制的调试。

问题#1 - 周缺少

周出现丢失,并且根据不同的日期,有时周缺失将对应于默认/当前日期(显示记录包含从数据库加载的日期),以及因此,当我显示 CalendarView ,并调用的setDate()来自动选择它,会出现无日期被选择(虽然 CalendarView 将围绕失踪一周为中心)。

路上,我一直在解决这个是手动向下滚动,然后备份了几个月,并刷新通常固定在显示器。但是,我还没有找到一种方法来自动完成这个滚动,这可能是围绕一个潜在的工作。我试着打电话的setDate()陆续要做到这一点,但它似乎只在第一次调用,这使我想到我的下一个问题的工作。

问题2 - 的setDate()自动滚动不工作

看来只有第一次调用的setDate()将导致 CalendarView 来围绕相应的中心日期。如果让我选择一个新的日期(并将其存储在私有成员),并关闭该弹出,然后将其带回了另一个下拉按钮点击,现在将调用的setDate()这个新的日期,那么 CalendarView 将围绕老/ previous日期为中心,即使新的日期实际上是hilited(可证实了我手动向下滚动到它)。

我可以将code如果需要的话,但花时间这样做之前,我只是想看看,如果这是一个众所周知的问题。

感谢您。


解决方案

这只是一个变通

通过首先与当月previous的最后一天所需的显示日期值调用的setDate(),然后用所需的日期调用它,它似乎一直按预期工作(务必在此日期该CalendarView的最大/最小日期)之间下降。

不过,第二次调用需要被张贴在一个可运行到UI线程。

下面是一些code(注布尔标志的setdate,为中心和滚动动画,两者都不工作,所以这种变通):

 查看= _inflater.Inflate(Resource.Layout.CalendarViewPicker,NULL);CalendarView cvPicker = view.FindViewById< Android.Widget.CalendarView>(Resource.Id.cv_picker);日期时间LASTDAY =新日期时间(_date.Year,_date.Month,1).AddDays(-1);
长lastDayTicks =蜱(lastDay.Date);cvPicker.SetDate(lastDayTicks,真实,真实);
cvPicker.Post(()=> {cvPicker.SetDate(蜱(_date.Date),真,真)});

传递_date.Date蜱()通过与上午12:00的时间的DateTime(基本上是零出时间因素),而不是仅仅通过_date,这也是一个DateTime。此外,需要一个转换功能,因为我使用C#/。NET,转换的蜱偏移

 私人长期蜱(DateTime的日期)
{
   日期时间_1970 =(新的DateTime(1970,1,1))AddDays(-1)。   返回(date.Ticks - _1970.Ticks)/ 10000;
}

Resource.Layout.CalendarViewPicker.axml:

 <?XML版本=1.0编码=UTF-8&GT?;
< RelativeLayout的
  的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
  机器人:方向=垂直
  机器人:layout_width =@扪/ calendar_view_width
  机器人:layout_height =@扪/ calendar_view_height
  机器人:背景=#FF666666>  <按钮
    机器人:ID =@ + ID / btn_picker_exit
    机器人:layout_width =60dp
    机器人:layout_height =36dp
    机器人:layout_alignParentTop =真
    机器人:背景=@绘制/ cv_button_background
    机器人:比重=中心
    机器人:文字颜色=#FFFFFFFF
    机器人:TEXTSIZE =20SP
    机器人:文字=+/>  < CalendarView
    机器人:ID =@ + ID / cv_picker
    机器人:layout_width =@扪/ calendar_view_width
    机器人:layout_height =@扪/ calendar_view_height
    机器人:shownWeekCount =6/>< RelativeLayout的/>

我对向的setdate第一次调用()与previous月的最后一天推理,是B / C多次日历将与表示作为第一个可见周某月的第二个星期弹出,和滚动最多见的第一个星期的时候,我会觉得缺少。所以,我想设置的previous月的最后一天的日期可能会迫使第一周(即本周有问题)来显示。

现在的第一周的表现,我再打电话的setdate,但它只能贴,B / C时发布可运行的UI时,该事件被定关联的视图渲染之后发生。如果2的setDate()的调用连续(既不是可运行),在OnCreate()中说,那么该视图呈现前效应发生,所以只有第二个电话会生效,覆盖第一个(因此失去迫使第一周显示)的效果。

这两个电话也解决集中/自动翻页的问题,这是很好的了。

I'm using a CalendarView directly, not a DatePicker, b/c its being used as a drop-down/pop-up style dialog where space is factor ( the user clicks a button located to the right of date field, and the CalendarView drops down, appearing directly below the field, aligned/anchored to the button).

There are two show-stopping issues I've ran into, and spent an entire 10 hour day debugging with no resolve.

Issue #1 - Weeks Missing

Weeks appear to missing, and depending on the date, sometimes the week missing will correspond to the "default"/current date (the date the displayed record contains that was loaded from the DB), and so when I display the CalendarView, and call setDate() to auto-select it, no date will appear to be selected (although the CalendarView will be centered around the missing week).

The way I've been fixing this is to manually scroll down, and then back up a few months, and the refresh usually fixes the display. However, I haven't found a way to automate this scrolling, which could be a potential work around. I tried calling setDate() in succession to do this, but it seems to only work on the first call, which brings me to my next issue.

Issue #2 - setDate() Auto-Scrolling Not Working

It seems that only the first call to setDate() will cause the CalendarView to be centered around the corresponding date. If I choose a new date (and store it in private member) and dismiss the popup, and then bring it back up with another dropdown-button click, which will now call setDate() with this new date, then the CalendarView will be centered around the old/previous date, even though the new date is actually hilited (which can be confirmed my manually scrolling down to it).

I can attach code if required, but before spending the time to do so, I just wanted to see if this is a well known issue.

Thank you.

解决方案

This is just a work-around (also, I've been working in Monodroid/C#, not raw Android)

By first calling SetDate() with a value for the last day of the month previous to the desired display date, and then calling it with the desired date, it seems to work consistently as expected (make sure this date falls between the Max/Min dates of the CalendarView).

However, the second call needs to be posted as a runnable to the UI thread.

Here is some code (note the boolean flags to SetDate, for centering and scroll-animation, neither of which work, hence this work-around):

View view = _inflater.Inflate( Resource.Layout.CalendarViewPicker, null );

CalendarView cvPicker = view.FindViewById<Android.Widget.CalendarView>( Resource.Id.cv_picker );

DateTime lastDay = new DateTime(_date.Year, _date.Month, 1).AddDays(-1);
long lastDayTicks = Ticks( lastDay.Date );

cvPicker.SetDate( lastDayTicks, true, true );
cvPicker.Post( () => {cvPicker.SetDate(Ticks(_date.Date),true,true)} );

Passing _date.Date to Ticks() passes a DateTime with a Time of 12:00am (basically "zeros out" time element), as opposed to passing just _date, which is also a DateTime. Also, a conversion function is needed, since I'm using C#/.NET, to convert the tick-offset

private long Ticks( DateTime date )
{
   DateTime _1970 = (new DateTime(1970, 1, 1)).AddDays(-1);

   return (date.Ticks - _1970.Ticks) / 10000;
}

Resource.Layout.CalendarViewPicker.axml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="@dimen/calendar_view_width"
  android:layout_height="@dimen/calendar_view_height"
  android:background="#FF666666">

  <Button
    android:id="@+id/btn_picker_exit"
    android:layout_width="60dp"
    android:layout_height="36dp"
    android:layout_alignParentTop="true"
    android:background="@drawable/cv_button_background"
    android:gravity="center"
    android:textColor="#FFFFFFFF"
    android:textSize="20sp"
    android:text="+"/>

  <CalendarView
    android:id="@+id/cv_picker"
    android:layout_width="@dimen/calendar_view_width"
    android:layout_height="@dimen/calendar_view_height"
    android:shownWeekCount="6"/>

<RelativeLayout/>

My reasoning for the first call to SetDate() with the last day of the previous month, was b/c many times the calendar would popup with the second week of a given month showing as the first visible week, and when scrolling up to see the first week, I would find it missing. So, I figured setting the date with the last day of the previous month might force the first week (ie, the problematic week) to show.

Now with the first week showing, I call SetDate again, but it only works when posted, b/c when posting a runnable to the UI, the event is scheduled to occur after the associated view is rendered. If 2 SetDate()'s are called consecutively (neither in a runnable), say in OnCreate(), then the effects take place before the view is rendered, and so only the second call would take effect, overwriting the first (and therefore loosing the effect of forcing the first week to show).

These two calls also fix the centering/auto-scrolling issue, which is nice too.

这篇关于CalendarView问题的时候直接使用(外一个datepicker的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 17:10