huanghaibin-dev / CalendarView

Android上一个优雅、万能自定义UI、仿iOS、支持垂直、水平方向切换、支持周视图、自定义周起始、性能高效的日历控件,支持热插拔实现的UI定制!支持标记、自定义颜色、农历、自定义月视图各种显示模式等。Canvas绘制,速度快、占用内存低,你真的想不到日历居然还可以如此优雅!An elegant, highly customized and high-performance Calendar Widget on Android.
Apache License 2.0
9.06k stars 1.78k forks source link

月视图转换到年视图,会有大约一秒延迟 #441

Open yccheok opened 5 years ago

yccheok commented 5 years ago

我是用以下代码从月视图转换到年视图

        mTextMonthYear.setOnClickListener(v -> {
            if (mCalendarView.isYearSelectLayoutVisible()) {
                return;
            }

            mCalendarView.showYearSelectLayout(mYear);
            mTextMonthYear.setText(String.valueOf(mYear));
        });

可是,我发现,转换时,性能不优化。会有大约一秒延迟。

1。现在在四月月视图 2。执行 mCalendarView.showYearSelectLayout(2019) 3。会有大约一秒延迟,才成功转换到2019年视图 4。点击四月,转换到四月月视图。没延迟。 5。执行 mCalendarView.showYearSelectLayout(2019) 6。成功转换到2019年视图。没延迟。

另一个 use case 如下

1。现在在四月月视图 2。执行 mCalendarView.showYearSelectLayout(2019) 3。会有大约一秒延迟,才成功转换到2019年视图 4。点击一月,转换到一月月视图。没延迟 5。执行 mCalendarView.showYearSelectLayout(2019) 6。会有大约一秒延迟,才成功转换到2019年视图

诡异的是,我在 CalendarView 的 Demo,并没发现这延迟的问题。

我看了看 CalendarView 的 Demo,并没发现 Demo 有作任何特别的处理。

我和 Demo 显著的不同是,我的年视图是 SCROLLABLE 的。不知,这是不是延迟的因素?

要体验延迟的效果,可以下载 https://wenote.me 看看

这是我的 XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="?attr/calendarBackgroundColor"
    android:orientation="vertical">

    <!-- navigation toolbar -->
    <RelativeLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="12dp"
        android:layout_marginRight="12dp"
        android:background="?attr/calendarBackgroundColor">

        <ImageButton
            android:id="@+id/previous_button"
            android:layout_width="64dp"
            android:layout_height="64dp"
            android:src="?attr/calendarLeftArrowIcon"
            android:background="?attr/actionBarItemBackground"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true" />

        <TextView
            android:id="@+id/month_year_text_view"
            android:layout_width="wrap_content"
            android:layout_height="64dp"
            android:gravity="center"
            android:textColor="?attr/primaryTextColor"
            android:textSize="24sp"
            android:textStyle="bold"
            android:layout_centerInParent="true"
            android:layout_centerVertical="true" />

        <ImageButton
            android:id="@+id/forward_button"
            android:layout_width="64dp"
            android:layout_height="64dp"
            android:src="?attr/calendarRightArrowIcon"
            android:background="?attr/actionBarItemBackground"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true" />

    </RelativeLayout>

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.haibin.calendarview.CalendarLayout
            android:id="@+id/calendar_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="?attr/calendarBackgroundColor"
            android:orientation="vertical"
            app:calendar_show_mode="both_month_week_view"
            app:default_status="expand"
            app:calendar_content_view_id="@+id/recycler_view">

            <com.haibin.calendarview.CalendarView

                android:layout_height="match_parent"
                app:calendar_padding="0dp"
                app:month_view_show_mode="mode_all"
                app:calendar_match_parent="true"

                android:id="@+id/calendar_view"
                android:layout_width="match_parent"
                android:background="?attr/calendarBackgroundColor"

                android:layout_marginLeft="0dp"
                android:layout_marginRight="0dp"

                app:month_view="com.yocto.wenote.calendar.CustomMonthView"
                app:week_view="com.yocto.wenote.calendar.CustomWeekView"

                app:current_month_text_color="?attr/calendarCurrentMonthTextColor"
                app:other_month_text_color="?attr/calendarOtherMonthTextColor"
                app:current_month_lunar_text_color="?attr/calendarCurrentMonthLunarTextColor"
                app:other_month_lunar_text_color="?attr/calendarOtherMonthLunarTextColor"

                app:day_text_size="16sp"
                app:current_day_text_color="?attr/calendarCurrentDayTextColor"
                app:current_day_lunar_text_color="?attr/calendarCurrentDayLunarTextColor"
                app:selected_text_color="?attr/calendarSelectedTextColor"
                app:selected_lunar_text_color="?attr/calendarSelectedLunarTextColor"
                app:selected_theme_color="?attr/calendarSelectedThemeColor"

                app:week_bar_view="com.yocto.wenote.calendar.FullscreenCustomWeekBar"
                app:week_background="?attr/calendarBackgroundColor"
                app:week_line_background="?attr/calendarWeekLineBackgroundColor"
                app:week_bar_height="46dp"
                app:week_start_with="sun"

                app:month_view_scrollable="false"
                app:week_view_scrollable="true"
                app:year_view_scrollable="false"

                app:year_view_background="?attr/calendarYearBackgroundColor"
                app:year_view_day_text_color="?attr/primaryTextColor"
                app:year_view_day_text_size="9sp"
                app:year_view_month_text_color="?attr/calendarYearMonthTextColor"
                app:year_view_month_text_size="20sp"
                app:year_view_scheme_color="?attr/calendarYearSchemeColor" />

            <androidx.recyclerview.widget.RecyclerView
                android:requiresFadingEdge="vertical"

                android:background="?attr/calendarBackgroundColor"
                android:overScrollMode="always"

                android:id="@+id/recycler_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:clipToPadding="false"
                android:paddingTop="0dp"
                android:paddingBottom="0dp" />
        </com.haibin.calendarview.CalendarLayout>

        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:visibility="gone"

            app:elevation="0dp"
            app:borderWidth="0dp"
            app:backgroundTint="@android:color/transparent"
            android:background="@android:color/transparent"

            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|center_horizontal"
            android:layout_margin="@dimen/fab_margin"
            android:src="?attr/tripleDownIcon" />

    </androidx.coordinatorlayout.widget.CoordinatorLayout>

</LinearLayout>

请问有什么见解吗?谢谢

huanghaibin-dev commented 5 years ago

YearViewPager需要重写2个方法

   @Override
    public void setCurrentItem(int item) {
        setCurrentItem(item, false);
    }

    @Override
    public void setCurrentItem(int item, boolean smoothScroll) {
        if (Math.abs(getCurrentItem() - item) > 1) {
            super.setCurrentItem(item, false);
        } else {
            super.setCurrentItem(item, false);
        }
    }
yccheok commented 5 years ago

对不起。我有些混淆。

这是最新 master branch 的 com.haibin.calendarview.YearViewPager 代码

    @Override
    public void setCurrentItem(int item) {
        setCurrentItem(item, false);
    }

    @Override
    public void setCurrentItem(int item, boolean smoothScroll) {
        if (Math.abs(getCurrentItem() - item) > 1) {
            super.setCurrentItem(item, false);
        } else {
            super.setCurrentItem(item, smoothScroll);
        }
    }

你是说要把他改去?

   @Override
    public void setCurrentItem(int item) {
        setCurrentItem(item, false);
    }

    @Override
    public void setCurrentItem(int item, boolean smoothScroll) {
        if (Math.abs(getCurrentItem() - item) > 1) {
            super.setCurrentItem(item, false);
        } else {
            super.setCurrentItem(item, false);
        }
    }

可是,我的代码,和官方版本 demo 代码,都是用着相同的 https://raw.githubusercontent.com/huanghaibin-dev/CalendarView/master/calendarview/src/main/java/com/haibin/calendarview/YearViewPager.java 。为何官方版本 demo 不会出现延迟的问题?谢谢

huanghaibin-dev commented 5 years ago

以前引起初始化慢是因为 setCurrentItem(item); 引起的, 最近几个版本通过 setCurrentItem(item, false); 得到了修复,你的原因并不是因为这个,可能是由于滚动视图引起的

zenghuiwang commented 4 years ago

请问这个问题解决了吗?