Android

[Kotlin] ViewPager2 code

YoYoHa 2021. 9. 14. 11:43
728x90
반응형

implementation "androidx.viewpager2:viewpager2:1.0.0"

 

 

viewpager.kt

class viewpager : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.viewpager)

        //뷰 페이저 셋팅
        val res = JSONArray()
        res.put("1Page")
        res.put("2Page")
        res.put("3Page")
        
        ads_viewPagerBox.adapter = RecyclerViewAdapter(res, this)
        
        // 관리 페이지 수 default = 1
        ads_viewPagerBox.offscreenPageLimit = 4
        
        val offsetBetweenPages = resources.getDimensionPixelOffset(R.dimen.offsetBetweenPages).toFloat()
        ads_viewPagerBox.setPageTransformer { 
            page, position -> val myOffset = position * -(2 * offsetBetweenPages)
            if (position < -1) {
              	page.translationX = -myOffset
            } else if (position <= 1) {
                val scaleFactor = 0.9f.coerceAtLeast(1 - abs(position))
                page.translationX = myOffset
                page.scaleY = scaleFactor
                page.alpha = scaleFactor
            } else {
                page.alpha = 0f
                page.translationX = myOffset
            }
        }
        ads_viewPagerBox.orientation = ViewPager2.ORIENTATION_HORIZONTAL
    }
}

 

RecyclerViewAdapter.kt

package com.example.viewpager

import android.R.attr.radius
import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import kotlinx.android.synthetic.main.item_view.view.*
import org.json.JSONArray
import java.text.DecimalFormat


class RecyclerViewAdapter(DataList: JSONArray, context: Context) : RecyclerView.Adapter<RecyclerViewAdapter.PagerViewHolder>() {

    var item = DataList
    var context = context

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = PagerViewHolder((parent))

    override fun getItemCount(): Int = item.length()

    @SuppressLint("SetTextI18n", "CheckResult")
    override fun onBindViewHolder(holder: PagerViewHolder, position: Int) {
        println(item.get(position))
        
		//셋팅
        holder.viewpager_title.text = item.get(position).toString()
    }

    inner class PagerViewHolder(parent: ViewGroup) : RecyclerView.ViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_view, parent, false)){
        val viewpager_title = itemView.viewpager_title
    }
}

 

 

 

viewpager.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/layout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:background="#000000"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".viewpager">
    
    <androidx.viewpager2.widget.ViewPager2
            android:id="@+id/ads_viewPagerBox"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_marginTop="16dp"
            android:layout_marginBottom="77dp"
            app:layout_constraintDimensionRatio="2:1.2"
            app:layout_constraintBottom_toTopOf="@+id/ads_button"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="parent" >

	</androidx.viewpager2.widget.ViewPager2>
</androidx.constraintlayout.widget.ConstraintLayout>

 

values/dimens.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!--  item_view 간의 양 옆 여백을 상쇄할 값   -->
    <dimen name="offsetBetweenPages">28dp</dimen>
    <!--  각 페이지의 양 옆 margin  -->
    <dimen name="pageMargin">36dp</dimen>
</resources>

 

item_view.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_marginLeft="@dimen/pageMargin"
    android:layout_marginRight="@dimen/pageMargin">

    <TextView
        android:id="@+id/viewpager_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:letterSpacing="-0.03"
        android:textColor="#92FCC9"
        android:textSize="50sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

 

 

빌드안해봐서 빨간줄 뜰수도? 있음

728x90
반응형