tKLGoogle Sample _ https://github.com/googlesamples/android-viewpager2
RecyclerView Aadapter를 이용한 pager
class Card private constructor(val title: String){
fun toBundle(): Bundle {
val args = Bundle(1)
args.putStringArray(TUTORIAL_ARGS_BUNDLE, arrayOf(title))
return args
}
companion object {
internal const val TUTORIAL_ARGS_BUNDLE = "TutorialArgsBundle"
val FM = arrayOf(Card("1"), Card("2"), Card("3"), Card("4"), Card("5"), Card("6"), Card("7"), Card("8"), Card("9"))
fun fromBundle(bundle: Bundle): Card {
val spec = bundle.getStringArray(TUTORIAL_ARGS_BUNDLE)
return Card(spec!![0])
}
}
}
전달할 정보 및 카드 인스턴스 생성
class CardView(layoutInflater: LayoutInflater, container: ViewGroup?) {
val view: View = layoutInflater.inflate(R.layout.test_fragment, container, false)
private val textSuite: TextView
init {
textSuite = view.findViewById(R.id.text)
}
fun bind(card: Card) {
textSuite.text = card.title
}
}
view 생성
여기서 두 가지 방법으로 나뉘는데
어답터 생성 후 viewpager.adapter = TutorialPagerAdapter() 를 이용하거나
class TutorialPagerAdapter : RecyclerView.Adapter<TutorialPagerAdapter.TutorialViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TutorialViewHolder {
return TutorialViewHolder(CardView(LayoutInflater.from(parent.context), parent))
}
override fun getItemCount(): Int {
return FM.size
}
override fun onBindViewHolder(holder: TutorialViewHolder, position: Int) {
holder.bind(FM[position])
}
class TutorialViewHolder internal constructor(private val cardView: CardView) :
RecyclerView.ViewHolder(cardView.view) {
internal fun bind(card: Card) {
cardView.bind(card)
}
}
}
그냥 FragmentStateAdapter(Fragmentactivity)를 이용하거나
viewPager.adapter = object : FragmentStateAdapter(this) {
override fun getItemCount(): Int {
return Card.FM.size
}
override fun createFragment(position: Int): Fragment {
return TutorialFragmet.create(Card.FM[position])
}
}
activity안에 fragment class를 만든건 처음이다
class TutorialFragmet : Fragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val cardView = CardView(layoutInflater, container)
cardView.bind(Card.fromBundle(arguments!!))
return cardView.view
}
companion object {
fun create(card: Card): TutorialFragmet {
val fragment = TutorialFragmet()
fragment.arguments = card.toBundle()
return fragment
}
}
}
Android indicator를 통하여 순서를 나타낼 수 있다
요로코롬한거?