Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

日历选择器怎么改成左右翻页? #288

Closed
palorotolo opened this issue Dec 31, 2021 · 7 comments
Closed

日历选择器怎么改成左右翻页? #288

palorotolo opened this issue Dec 31, 2021 · 7 comments

Comments

@palorotolo
Copy link

类似 viewpager 的那种。按钮左右切换月份。

@liyujiang-gzu
Copy link
Member

viewpager 的那种左右切换月份,改动会比较大。将CalendarView中的androidx.recyclerview.widget.RecyclerView改为androidx.viewpager2.widget.ViewPager2可能会小一些,毕竟ViewPager2是基于RecyclerView的。

@palorotolo
Copy link
Author

val calendarView = CalendarView(requireContext())
            val minDate = Date(System.currentTimeMillis())
            val calendar = DateUtils.calendar(minDate)
            calendar.add(Calendar.MONTH, 3)
            val maxDate = calendar.time
            RecyclerView(requireContext()).adapter = calendarView.adapter
            calendarView.adapter
                .notify(false)
                .single(false)
                .valid(minDate, maxDate)
                .intervalNotes("开始", "结束")
                .select(minDate.time, minDate.time)
                .range(minDate, maxDate)
                .refresh()
            val dateSelectionList = contentView.findViewById<RecyclerView>(R.id.dateSelectionList)
            dateSelectionList.layoutManager =
                LinearLayoutManager(requireContext(), RecyclerView.HORIZONTAL, false)
            PagerSnapHelper().attachToRecyclerView(dateSelectionList)
            dateSelectionList.adapter = calendarView.adapter

这样写差不多实现了。

@liyujiang-gzu
Copy link
Member

val calendarView = CalendarView(requireContext())
            val minDate = Date(System.currentTimeMillis())
            val calendar = DateUtils.calendar(minDate)
            calendar.add(Calendar.MONTH, 3)
            val maxDate = calendar.time
            RecyclerView(requireContext()).adapter = calendarView.adapter
            calendarView.adapter
                .notify(false)
                .single(false)
                .valid(minDate, maxDate)
                .intervalNotes("开始", "结束")
                .select(minDate.time, minDate.time)
                .range(minDate, maxDate)
                .refresh()
            val dateSelectionList = contentView.findViewById<RecyclerView>(R.id.dateSelectionList)
            dateSelectionList.layoutManager =
                LinearLayoutManager(requireContext(), RecyclerView.HORIZONTAL, false)
            PagerSnapHelper().attachToRecyclerView(dateSelectionList)
            dateSelectionList.adapter = calendarView.adapter

这样写差不多实现了。

思路很新奇啊,把竖向的改为横行的

@palorotolo
Copy link
Author

但还是不完美,不方便监听当前月份的变化。在改别的日历框架。

@liyujiang-gzu
Copy link
Member

PagerSnapHelper().attachToRecyclerView(dateSelectionList)

你再用RecyclerView来重新包装目测是多于的,可以通过calendarView.getBodyView()获取到RecyclerView

        RecyclerView bodyView = calendarView.getBodyView();
        bodyView.setLayoutManager(new LinearLayoutManager(getContext(), RecyclerView.HORIZONTAL, false));
        new PagerSnapHelper().attachToRecyclerView(bodyView);

@liyujiang-gzu
Copy link
Member

但还是不完美,不方便监听当前月份的变化。在改别的日历框架。

不用RecyclerView来重新包装,直接获取CalendarView自带的RecyclerView,应该也就不存在不方便监听当前月份变化的问题了

@liyujiang-gzu
Copy link
Member

liyujiang-gzu commented Jan 5, 2022

但还是不完美,不方便监听当前月份的变化。在改别的日历框架。

不用RecyclerView来重新包装,直接获取CalendarView自带的RecyclerView,应该也就不存在不方便监听当前月份变化的问题了

@palorotolo 效果还行吧:

        CalendarPicker picker = new CalendarPicker(this);
        picker.setHeight(CalendarPicker.WRAP_CONTENT);
        picker.setRangeDateOnFuture(3);
        picker.setFestivalProvider(new MyFestivalProvider());
        picker.setOnSingleDatePickListener(new OnSingleDatePickListener() {
            @Override
            public void onSingleDatePicked(@NonNull Date date) {
                singleTimeInMillis = date.getTime();
                Toast.makeText(getApplicationContext(), DateFormat.getDateTimeInstance().format(date), Toast.LENGTH_SHORT).show();
            }
        });
        picker.show();
        RecyclerView bodyView = picker.getCalendarView().getBodyView();
        bodyView.setLayoutManager(new LinearLayoutManager(this, RecyclerView.HORIZONTAL, false));
        new PagerSnapHelper().attachToRecyclerView(bodyView);

liyujiang-gzu added a commit that referenced this issue Jan 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants