Nail nail has what function, millet calendar and iOS calendar are the same, too lazy to record the screen is I borrow iOS colleagues, this is the Android version of iOS go out right turnMock nail calendar page, day view under the day schedule viewThe principle of iOS to see the article, but the most important sorting algorithm is different.

Rely on

Add repositories to the project root directory (build.gradle)

	allprojects {
		repositories {
			maven { url 'https://jitpack.io'}}}Copy the code

Add dependencies to build. Gradle

	dependencies {
		implementation 'com. Making. Wittyneko: ScheduleView: 1.0.0'
	}
Copy the code

use

Define Adapter

class ScheduleAdapter(
    private val view: ScheduleView
) : ScheduleView.Adapter<Triple<Period, Period, String>>() {

    val list = mutableListOf<Triple<Period, Period, String>>()

    init {
        list.addAll(
            arrayOf(
                Triple(Period.hours(3), Period.hours(4), "Schedule 1"),
                Triple(Period.hours(3).withMinutes(30), Period.hours(4).withMinutes(30), "Schedule 2"),
                Triple(Period.hours(3).withMinutes(45), Period.hours(5), "Schedule 3"),
                Triple(Period.hours(5).withMinutes(30), Period.hours(7), "Schedule 4"),
                Triple(Period.hours(7), Period.hours(9), "Schedule 5"),
                Triple(Period.hours(7).withMinutes(8), Period.hours(9), "Schedule 6")))}override fun getItemCount(a): Int = list.size

    override fun getItem(position: Int): Triple<Period, Period, String> = list[position]

    override fun bindView(item: Triple<Period, Period, String>, view: ScheduleItem) {
        view.tvContent.text = item.third
        view.startPeriod = item.first
        view.endPeriod = item.second
    }

    override fun bindEdit(item: Triple<Period, Period, String>, view: ScheduleEdit) {
        view.tvContent.text = item.third
        view.startPeriod = item.first
        view.endPeriod = item.second
    }

    override fun bindCreate(view: ScheduleEdit) {
        view.tvContent.text = "New Schedule"
    }

    fun notifyAllChange(a) {
        // Edit Item first exit edit
        if (view.editView.isShow) {
            view.cancelEdit()
        }
        view.notifyAllItem()
    }
}
Copy the code

Bind data and listen

    private val scheduleView by lazy { findViewById<ScheduleView>(R.id.schedule_view) }

    private val adapter by lazy { ScheduleAdapter(scheduleView) }

    // Schedule creation
    private val onCreateClickListener = View.OnClickListener {
        val edit = it.asView<ScheduleEdit>()
        adapter.list.apply {
            add(Triple(edit.startPeriod, edit.endPeriod, "Plan${size + 1}"))
        }
        adapter.notifyAllChange()
    }

    // Schedule click listen
    private val onItemClickListener = { view: ScheduleItem, position: Int ->
        Toast.makeText(this."${view.tvContent.text}", Toast.LENGTH_SHORT).show()
    }

    // Schedule change listener
    private val onItemChangeListener = listener@{ view: ScheduleItem, position: Int ->
        val item = adapter.list[position]
        adapter.list[position] = item.copy(view.startPeriod, view.endPeriod)
        adapter.notifyAllChange()
    }

    private fun initScheduleView(a) {
        scheduleView.onCreateClickListener = onCreateClickListener
        scheduleView.onItemClickListener = onItemClickListener
        scheduleView.onItemChangeListener = onItemChangeListener
        scheduleView.setAdapter(adapter)
        adapter.notifyAllChange()
    }
Copy the code

The layout must be placed in NestedScrollView

    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.wittyneko.schedule.widget.ScheduleView
            android:id="@+id/schedule_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </androidx.core.widget.NestedScrollView>
Copy the code

Source address: ScheduleView