Upgrade to Pro — share decks privately, control downloads, hide ads and more …

How to implement TimetablelLayoutManager

How to implement TimetablelLayoutManager

Moyuru Aizawa

March 06, 2019
Tweet

More Decks by Moyuru Aizawa

Other Decks in Programming

Transcript

  1. ‣ "EBQUFS ‣ -BZPVU.BOBHFS ‣ *UFN%FDPSBUJPO ‣ *UFN"OJNBUPS ‣ *UFN5PVDI)FMQFS

    ‣ 4OBQ)FMQFS ‣ 3FDZDMFE7JFX1PPM ‣ %JGG6UJM 3FDZDMFS7JFXͱ͸
  2. ‣ PO-BZPVU$IJMESFO ‣ TDSPMM7FSUJDBMMZ#ZTDSPMM)PSJ[POUBMMZ#Z ‣ TDSPMM5P ‣ TNPPUI4DSPMM5P ‣ DPNQVUF7FSUJDBM4DSPMM&YUFOUDPNQVUF)PSJ[POUBM4DSPMM&YUFOU

    ‣ DPNQVUF7FSUJDBM4DSPMM0GGTFUDPNQVUF)PSJ[POUBM4DSPMM0GGTFU ‣ DPNQVUF7FSUJDBM4DSPMM3BOHFDPNQVUF)PSJ[POUBM4DSPMM3BOHF ‣ *UFN1SFGFUDI ‣ *UFN%FDPSBUJPOT ‣ *UFN"OJNBUPST ‣ 1SFEJDUJWF"OJNBUJPOT ‣ ʜ -BZPVU.BOBHFSͷؔ਺ػೳ
  3. ‣ PO-BZPVU$IJMESFO ‣ TDSPMM7FSUJDBMMZ#ZTDSPMM)PSJ[POUBMMZ#Z ‣ TDSPMM5P ‣ TNPPUI4DSPMM5P ‣ DPNQVUF7FSUJDBM4DSPMM&YUFOUDPNQVUF)PSJ[POUBM4DSPMM&YUFOU

    ‣ DPNQVUF7FSUJDBM4DSPMM0GGTFUDPNQVUF)PSJ[POUBM4DSPMM0GGTFU ‣ DPNQVUF7FSUJDBM4DSPMM3BOHFDPNQVUF)PSJ[POUBM4DSPMM3BOHF ‣ *UFN1SFGFUDI ‣ *UFN%FDPSBUJPOT ‣ *UFN"OJNBUPST ‣ 1SFEJDUJWF"OJNBUJPOT ‣ ʜ -BZP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ount).forEach { val periodInfo = periodLookUp(it) val column

    = columns.getOrPut(periodInfo.columnNumber) { ArrayList() } val period = Period( TimeUnit.MILLISECONDS.toMinutes(periodInfo.startUnixMillis).toInt(), TimeUnit.MILLISECONDS.toMinutes(periodInfo.endUnixMillis).toInt(), periodInfo.columnNumber, adapterPosition = it, positionInColumn = column.size ) periods.add(period) column.add(period) if (it == 0) { firstStartUnixMin = period.startUnixMin lastEndUnixMin = period.endUnixMin } else { firstStartUnixMin = min(period.startUnixMin, firstStartUnixMin) lastEndUnixMin = max(period.endUnixMin, lastEndUnixMin) } } ֤QFSJPEͷ৘ใऩू
  13. (0 until itemCount).forEach { val periodInfo = periodLookUp(it) val column

    = columns.getOrPut(periodInfo.columnNumber) { ArrayList() } val period = Period( TimeUnit.MILLISECONDS.toMinutes(periodInfo.startUnixMillis).toInt(), TimeUnit.MILLISECONDS.toMinutes(periodInfo.endUnixMillis).toInt(), periodInfo.columnNumber, adapterPosition = it, positionInColumn = column.size ) periods.add(period) column.add(period) if (it == 0) { firstStartUnixMin = period.startUnixMin lastEndUnixMin = period.endUnixMin } else { firstStartUnixMin = min(period.startUnixMin, firstStartUnixMin) lastEndUnixMin = max(period.endUnixMin, lastEndUnixMin) } } ֤QFSJPEͷ৘ใऩू
  14. (0 until itemCount).forEach { val periodInfo = periodLookUp(it) val column

    = columns.getOrPut(periodInfo.columnNumber) { ArrayList() } val period = Period( TimeUnit.MILLISECONDS.toMinutes(periodInfo.startUnixMillis).toInt(), TimeUnit.MILLISECONDS.toMinutes(periodInfo.endUnixMillis).toInt(), periodInfo.columnNumber, adapterPosition = it, positionInColumn = column.size ) periods.add(period) column.add(period) if (it == 0) { firstStartUnixMin = period.startUnixMin lastEndUnixMin = period.endUnixMin } else { firstStartUnixMin = min(period.startUnixMin, firstStartUnixMin) lastEndUnixMin = max(period.endUnixMin, lastEndUnixMin) } } ֤QFSJPEͷ৘ใऩू
  15. anchor.leftColumn = 0 val columnCount = columns.size() val offsetY =

    parentTop var offsetX = parentLeft for (columnNumber in 0 until columnCount) { offsetX += addColumn( columns[columnNumber].first(), offsetX, offsetY, true, recycler) anchor.rightColumn = columnNumber if (offsetX > parentRight) break } *OJUJBMMBZPVU
  16. val range = startPeriod.positionInColumn until column.size for (i in range)

    { val period = column[i] val (width, height) = addPeriod(period, direction, offsetX, offsetY, recycler) offsetY += height columnWidth = width if (i == startPeriod.positionInColumn) anchor.top.put(columnNum, period.adapterPosition) anchor.bottom.put(columnNum, period.adapterPosition) if (offsetY > parentBottom) break } *OJUJBMMBZPVU
  17. val range = startPeriod.positionInColumn until column.size for (i in range)

    { val period = column[i] val (width, height) = addPeriod(period, direction, offsetX, offsetY, recycler) offsetY += height columnWidth = width if (i == startPeriod.positionInColumn) anchor.top.put(columnNum, period.adapterPosition) anchor.bottom.put(columnNum, period.adapterPosition) if (offsetY > parentBottom) break } *OJUJBMMBZPVU
  18. anchor.leftColumn = 0 val columnCount = columns.size() val offsetY =

    parentTop var offsetX = parentLeft for (columnNumber in 0 until columnCount) { offsetX += addColumn( columns[columnNumber].first(), offsetX, offsetY, true, recycler) anchor.rightColumn = columnNumber if (offsetX > parentRight) break } *OJUJBMMBZPVU
  19. val bottomView = findBottomView() ?: return 0 val period =

    periods.getOrNull(bottomView.adapterPosition) ?: return 0 val bottom = getDecoratedBottom(bottomView) if (period.endUnixMin == lastEndUnixMin) if (bottom == parentBottom) 0 else min(dy, bottom - parentBottom) else dy εΫϩʔϧྔͷܭࢉ
  20. val bottomView = findBottomView() ?: return 0 val period =

    periods.getOrNull(bottomView.adapterPosition) ?: return 0 val bottom = getDecoratedBottom(bottomView) if (period.endUnixMin == lastEndUnixMin) if (bottom == parentBottom) 0 else min(dy, bottom - parentBottom) else dy εΫϩʔϧྔͷܭࢉ
  21. val bottomView = findBottomView() ?: return 0 val period =

    periods.getOrNull(bottomView.adapterPosition) ?: return 0 val bottom = getDecoratedBottom(bottomView) if (period.endUnixMin == lastEndUnixMin) if (bottom == parentBottom) 0 else min(dy, bottom - parentBottom) else dy εΫϩʔϧྔͷܭࢉ
  22. val bottomView = findBottomView() ?: return 0 val period =

    periods.getOrNull(bottomView.adapterPosition) ?: return 0 val bottom = getDecoratedBottom(bottomView) if (period.endUnixMin == lastEndUnixMin) if (bottom == parentBottom) 0 else min(dy, bottom - parentBottom) else dy εΫϩʔϧྔͷܭࢉ
  23. override fun scrollVerticallyBy( dy: Int, recycler: Recycler, state: State ):

    Int { if (dy == 0) return 0 val actualDy = calculateVerticallyScrollAmount(dy) if (actualDy == 0) return 0 offsetChildrenVertical(-actualDy) … } $IJMEWJFXTͷҠಈ
  24. override fun scrollVerticallyBy( dy: Int, recycler: Recycler, state: State ):

    Int { if (dy == 0) return 0 val actualDy = calculateVerticallyScrollAmount(dy) if (actualDy == 0) return 0 offsetChildrenVertical(-actualDy) … } $IJMEWJFXTͷҠಈ
  25. (anchor.leftColumn..anchor.rightColumn) .forEach { columnNum -> val column = columns[columnNum] val

    top = periods[anchor.top[columnNum]] val bottom = periods[anchor.bottom[columnNum]] column.subList( top.positionInColumn, bottom.positionInColumn ).forEach { val view = findViewByPosition(it.adapterPosition)?: return if (getDecoratedBottom(view) >= parentTop) return removeAndRecycleView(view, recycler) val belowPosition = column[it.positionInColumn + 1] .adapterPosition anchor.top.put(columnNum, belowPosition) } } ը໘͔ΒͰͨDIJMEWJFXͷ࡟আ
  26. (anchor.leftColumn..anchor.rightColumn) .forEach { columnNum -> val column = columns[columnNum] val

    top = periods[anchor.top[columnNum]] val bottom = periods[anchor.bottom[columnNum]] column.subList( top.positionInColumn, bottom.positionInColumn ).forEach { val view = findViewByPosition(it.adapterPosition)?: return if (getDecoratedBottom(view) >= parentTop) return removeAndRecycleView(view, recycler) val belowPosition = column[it.positionInColumn + 1] .adapterPosition anchor.top.put(columnNum, belowPosition) } } ը໘͔ΒͰͨDIJMEWJFXͷ࡟আ
  27. (anchor.leftColumn..anchor.rightColumn) .forEach { columnNum -> val column = columns[columnNum] val

    top = periods[anchor.top[columnNum]] val bottom = periods[anchor.bottom[columnNum]] column.subList( top.positionInColumn, bottom.positionInColumn ).forEach { val view = findViewByPosition(it.adapterPosition)?: return if (getDecoratedBottom(view) >= parentTop) return removeAndRecycleView(view, recycler) val belowPosition = column[it.positionInColumn + 1] .adapterPosition anchor.top.put(columnNum, belowPosition) } } ը໘͔ΒͰͨDIJMEWJFXͷ࡟আ
  28. (anchor.leftColumn..anchor.rightColumn) .forEach { columnNum -> val column = columns[columnNum] val

    top = periods[anchor.top[columnNum]] val bottom = periods[anchor.bottom[columnNum]] column.subList( top.positionInColumn, bottom.positionInColumn ).forEach { val view = findViewByPosition(it.adapterPosition)?: return if (getDecoratedBottom(view) >= parentTop) return removeAndRecycleView(view, recycler) val belowPosition = column[it.positionInColumn + 1] .adapterPosition anchor.top.put(columnNum, belowPosition) } } ը໘͔ΒͰͨDIJMEWJFXͷ࡟আ
  29. (anchor.leftColumn..anchor.rightColumn) .forEach { columnNum -> val column = columns[columnNum] val

    top = periods[anchor.top[columnNum]] val bottom = periods[anchor.bottom[columnNum]] column.subList( top.positionInColumn, bottom.positionInColumn ).forEach { val view = findViewByPosition(it.adapterPosition)?: return if (getDecoratedBottom(view) >= parentTop) return removeAndRecycleView(view, recycler) val belowPosition = column[it.positionInColumn + 1] .adapterPosition anchor.top.put(columnNum, belowPosition) } } ը໘͔ΒͰͨDIJMEWJFXͷ࡟আ
  30. anchor.bottom.forEach { columnNum, position -> val view = findViewByPosition(position) ?:

    return@forEach val bottom = getDecoratedBottom(view) if (bottom < parentBottom) { val left = getDecoratedLeft(view) val period = periods.getOrNull(position) ?: return@forEach val nextPeriod = columns.get(columnNum) .getOrNull(period.positionInColumn + 1) ?: return@forEach addPeriodsToColumn(nextPeriod, left, bottom, true, recycler) } } ۭ͖εϖʔεʹWJFXΛ௥Ճ
  31. anchor.bottom.forEach { columnNum, position -> val view = findViewByPosition(position) ?:

    return@forEach val bottom = getDecoratedBottom(view) if (bottom < parentBottom) { val left = getDecoratedLeft(view) val period = periods.getOrNull(position) ?: return@forEach val nextPeriod = columns.get(columnNum) .getOrNull(period.positionInColumn + 1) ?: return@forEach addPeriodsToColumn(nextPeriod, left, bottom, true, recycler) } } ۭ͖εϖʔεʹWJFXΛ௥Ճ
  32. anchor.bottom.forEach { columnNum, position -> val view = findViewByPosition(position) ?:

    return@forEach val bottom = getDecoratedBottom(view) if (bottom < parentBottom) { val left = getDecoratedLeft(view) val period = periods.getOrNull(position) ?: return@forEach val nextPeriod = columns.get(columnNum) .getOrNull(period.positionInColumn + 1) ?: return@forEach addPeriodsToColumn(nextPeriod, left, bottom, true, recycler) } } ۭ͖εϖʔεʹWJFXΛ௥Ճ
  33. anchor.bottom.forEach { columnNum, position -> val view = findViewByPosition(position) ?:

    return@forEach val bottom = getDecoratedBottom(view) if (bottom < parentBottom) { val left = getDecoratedLeft(view) val period = periods.getOrNull(position) ?: return@forEach val nextPeriod = columns.get(columnNum) .getOrNull(period.positionInColumn + 1) ?: return@forEach addPeriodsToColumn(nextPeriod, left, bottom, true, recycler) } } ۭ͖εϖʔεʹWJFXΛ௥Ճ