Kotlin × DataBinding × ViewPager를 사용하여 자습서 화면 만들기

ViewPager를 사용하여 이미지 시스템의 자습서 화면을 스크롤합니다.🚀
원래의 강좌는 완성 단추와 지시기가 있어야 하지만, 우선 그림을 굴리는 기본적인 실현이다.

MainActivity.kt

package com.shanonim.viewpagersample

import android.databinding.DataBindingUtil
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import com.shanonim.viewpagersample.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {

    lateinit private var binding: ActivityMainBinding
    private val images: IntArray = intArrayOf(
            R.drawable.tutorial_01,
            R.drawable.tutorial_02,
            R.drawable.tutorial_03,
            R.drawable.tutorial_04,
            R.drawable.tutorial_05
    )

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
        binding.viewPager.adapter = CustomPagerAdapter(this, images)
    }
}

CustomPagerAdapter.kt

package com.shanonim.viewpagersample

import android.content.Context
import android.support.v4.view.PagerAdapter
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import com.shanonim.viewpagersample.databinding.ItemTutorialBinding

class CustomPagerAdapter(val context: Context, val images: IntArray) : PagerAdapter() {

    override fun getCount(): Int = images.size

    override fun isViewFromObject(view: View?, `object`: Any?): Boolean {
        return view == `object`
    }

    override fun instantiateItem(container: ViewGroup?, position: Int): Any {
        val binding = ItemTutorialBinding.inflate(LayoutInflater.from(context), container, false)
        binding.imageView.setImageResource(images[position])

        container?.addView(binding.root)
        return binding.root
    }

    override fun destroyItem(container: ViewGroup?, position: Int, `object`: Any?) {
        container?.removeView(`object` as LinearLayout)
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.shanonim.viewpagersample.MainActivity">

        <android.support.v4.view.ViewPager
            android:id="@+id/view_pager"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </RelativeLayout>
</layout>

item_view_pager.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/image_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </LinearLayout>
</layout>

총결산

  • Kotlin 최고🎉
  • 웹 페이지 정보

  • Android Image Slider Using ViewPager Example
    http://www.thecrazyprogrammer.com/2016/12/android-image-slider-using-viewpager-example.html
  • 좋은 웹페이지 즐겨찾기