predicate にマッチする最初の位置の index を返す。マッチするものが無い場合は -1 を返す。
Java
- @Override
- public int getSectionForPosition(int position) {
- final Object[] sections = getSections();
- if (sections == null) {
- return 0;
- }
- int section = 0;
- for (int i = 0; i < sections.length; i++) {
- final MonthSection ms = (MonthSection) sections[i];
- if (ms.position > position) {
- return section;
- }
- section = i;
- }
- return section;
- }
- override fun getSectionForPosition(position: Int): Int {
- val sections = getSections() ?: return 0
- return sections.indexOfFirst { it.position > position }
- .let {
- when {
- it > 0 -> it - 1
- it == 0 -> 0
- else -> sections.lastIndex
- }
- }
- }
0 件のコメント:
コメントを投稿