Created
July 19, 2019 08:31
-
-
Save hixguru/d0155e1d3d42499a2b000cbe5f8cf530 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package co.riiid.vida.main.study.widget | |
import android.view.ViewGroup | |
import androidx.fragment.app.Fragment | |
import androidx.fragment.app.FragmentManager | |
import androidx.fragment.app.FragmentStatePagerAdapter | |
import androidx.viewpager.widget.ViewPager | |
class MainWidgetAdapter(fm: FragmentManager) : FragmentStatePagerAdapter(fm) { | |
companion object { | |
const val FRAGMENT_SIZE = 7 | |
const val LOOP_COUNT = Int.MAX_VALUE | |
/** | |
* 첫 페이지를 표시하기 위한 중간 부분의 위치. | |
*/ | |
const val INITIAL_POS = LOOP_COUNT / 2 - (LOOP_COUNT / 2 % FRAGMENT_SIZE) | |
} | |
override fun getItem(position: Int): Fragment { | |
return when (compressedPos(position)) { | |
0 -> TodayStatusFragment.newInstance() | |
1 -> ScoreFragment.newInstance() | |
2 -> LcRcFragment.newInstance() | |
3 -> GradeFragment.newInstance() | |
4 -> ComboFragment.newInstance() | |
5 -> DDayFragment.newInstance() | |
6 -> CoinFragment.newInstance() | |
else -> throw IndexOutOfBoundsException() | |
} | |
} | |
override fun getCount(): Int { | |
return LOOP_COUNT | |
} | |
val compressedCount: Int | |
get() = FRAGMENT_SIZE | |
fun inCompressedBounds(position: Int): Boolean = | |
compressedPos(position) in (0 until FRAGMENT_SIZE) | |
override fun instantiateItem(container: ViewGroup, position: Int): Any { | |
return super.instantiateItem(container, compressedPos(position)) | |
} | |
override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) { | |
super.destroyItem(container, compressedPos(position), `object`) | |
} | |
fun getFragment(container: ViewGroup, position: Int): WidgetFragment? = | |
instantiateItem(container, position) as? WidgetFragment | |
private fun compressedPos(position: Int): Int = position % FRAGMENT_SIZE | |
} | |
internal val ViewPager.compressedCurrentItem: Int | |
get() = currentItem.rem(MainWidgetAdapter.FRAGMENT_SIZE) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment