신기성

viewpager demo create

...@@ -3,10 +3,12 @@ package com.example.vip ...@@ -3,10 +3,12 @@ package com.example.vip
3 import android.os.Bundle 3 import android.os.Bundle
4 import androidx.appcompat.app.AppCompatActivity 4 import androidx.appcompat.app.AppCompatActivity
5 import androidx.core.content.ContextCompat 5 import androidx.core.content.ContextCompat
6 +import androidx.viewpager.widget.ViewPager
6 import kotlinx.android.synthetic.main.activity_signin.* 7 import kotlinx.android.synthetic.main.activity_signin.*
7 8
8 class SignInActivity : AppCompatActivity() { 9 class SignInActivity : AppCompatActivity() {
9 10
11 + internal lateinit var viewpager : ViewPager
10 override fun onCreate(savedInstanceState: Bundle?) { 12 override fun onCreate(savedInstanceState: Bundle?) {
11 super.onCreate(savedInstanceState) 13 super.onCreate(savedInstanceState)
12 setContentView(R.layout.activity_signin) 14 setContentView(R.layout.activity_signin)
...@@ -27,6 +29,9 @@ class SignInActivity : AppCompatActivity() { ...@@ -27,6 +29,9 @@ class SignInActivity : AppCompatActivity() {
27 val adapter = PolicyFieldAdapter(policyFieldList) 29 val adapter = PolicyFieldAdapter(policyFieldList)
28 policyFieldRecyclerView.adapter = adapter 30 policyFieldRecyclerView.adapter = adapter
29 31
32 + viewpager=findViewById(R.id.viewpager) as ViewPager
33 + val viewpageradapter = ViewPagerAdapter(this)
34 + viewpager.adapter=viewpageradapter
30 } 35 }
31 36
32 37
......
1 +package com.example.vip
2 +
3 +import android.content.Context
4 +import android.view.LayoutInflater
5 +import android.view.View
6 +import android.view.ViewGroup
7 +import android.widget.ImageView
8 +import androidx.viewpager.widget.PagerAdapter
9 +import androidx.viewpager.widget.ViewPager
10 +
11 +class ViewPagerAdapter(private val context : Context) : PagerAdapter() {
12 +
13 + private var layoutInflater : LayoutInflater? = null
14 + val Image = arrayOf(
15 + R.drawable.image01,
16 + R.drawable.image02,
17 + R.drawable.image03
18 + )
19 +
20 +
21 + override fun isViewFromObject(view: View, `object`: Any): Boolean {
22 + return view === `object`
23 + }
24 +
25 + override fun getCount(): Int {
26 + return Image.size
27 + }
28 +
29 +
30 + override fun instantiateItem(container: ViewGroup, position: Int): Any {
31 + layoutInflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
32 + val v = layoutInflater!!.inflate(R.layout.viewpager_activity, null)
33 + val image = v.findViewById<View>(R.id.imageview) as ImageView
34 +
35 + image.setImageResource(Image[position])
36 + val vp = container as ViewPager
37 + vp.addView(v , 0)
38 +
39 +
40 + return v
41 +
42 + }
43 +
44 +
45 + override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
46 + val vp = container as ViewPager
47 + val v = `object` as View
48 + vp.removeView(v)
49 + }
50 +}
...\ No newline at end of file ...\ No newline at end of file
...@@ -19,11 +19,16 @@ ...@@ -19,11 +19,16 @@
19 android:layout_height="50dp" 19 android:layout_height="50dp"
20 android:text="로고 및 검색창" /> 20 android:text="로고 및 검색창" />
21 21
22 - <TextView 22 + <androidx.viewpager.widget.ViewPager
23 - android:id="@+id/space_rollingBanner" 23 + android:id="@+id/viewpager"
24 - android:layout_width="match_parent" 24 + android:layout_width="436dp"
25 - android:layout_height="200dp" 25 + android:layout_height="220dp"
26 - android:text="롤링배너창" /> 26 + app:layout_constraintStart_toStartOf="parent"
27 + android:layout_marginStart="8dp"
28 + app:layout_constraintTop_toTopOf="parent"
29 + app:layout_constraintEnd_toEndOf="parent"
30 + android:layout_marginEnd="8dp">
31 + </androidx.viewpager.widget.ViewPager>
27 32
28 <androidx.recyclerview.widget.RecyclerView 33 <androidx.recyclerview.widget.RecyclerView
29 android:id="@+id/policyFieldRecyclerView" 34 android:id="@+id/policyFieldRecyclerView"
......
1 +<?xml version="1.0" encoding="utf-8"?>
2 +<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 + xmlns:app="http://schemas.android.com/apk/res-auto"
4 + xmlns:tools="http://schemas.android.com/tools"
5 + android:orientation="vertical" android:layout_width="match_parent"
6 + android:layout_height="match_parent">
7 +
8 + <ImageView
9 + android:id="@+id/imageview"
10 + android:layout_width="match_parent"
11 + android:layout_height="wrap_content"
12 + tools:srcCompat="@tools:sample/avatars" />
13 +</LinearLayout>
...\ No newline at end of file ...\ No newline at end of file