Kotlin × DataBinding × ViewPager를 사용하여 자습서 화면 만들기
10753 단어 AndroidKotlinDataBinding
원래의 강좌는 완성 단추와 지시기가 있어야 하지만, 우선 그림을 굴리는 기본적인 실현이다.
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>
총결산
웹 페이지 정보
http://www.thecrazyprogrammer.com/2016/12/android-image-slider-using-viewpager-example.html
Reference
이 문제에 관하여(Kotlin × DataBinding × ViewPager를 사용하여 자습서 화면 만들기), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/shanonim/items/4bf31a203cc90d73aae8텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)