[Android/Kotlin] RecyclerView Animation ํ™œ์šฉํ•˜๊ธฐ

2021. 1. 22. 01:34ใ†๐Ÿ“ฑAndroid/๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป Android ๊ฐœ๋ฐœ ์ผ์ง€

๋ฐ˜์‘ํ˜•

 

์ €๋ฒˆ RecyclerView ๋งŒ๋“ค๊ธฐ์™€ ClickEvent์— ์ด์–ด ํ™”๋ฉด ์ „ํ™˜์‹œ RecyclerView์— ๊ฐ„๋‹จํ•œ Animation์„ ์ ์šฉํ•ด๋ณด๋Š” ๊ฒƒ์— ๋Œ€ํ•ด ํฌ์ŠคํŒ… ํ•ด๋ณด๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค.

์ €๋ฒˆ ํฌ์ŠคํŒ…๊ณผ ์ด์–ด์ง€๊ธฐ ๋•Œ๋ฌธ์— ์•„๋ž˜ ๊ธ€์„ ๋จผ์ € ์ฝ๊ณ  ์˜ค์‹œ๋Š” ๊ฒƒ์„ ์ถ”์ฒœ๋“œ๋ฆฝ๋‹ˆ๋‹ค : )

 

[Android/Kotlin] RecyclerView ๋งŒ๋“ค๊ธฐ

์˜ค๋Š˜์€ ๊ฐ„๋‹จํ•œ ๋ฆฌ์‚ฌ์ดํด๋Ÿฌ๋ทฐ ์‹œ๋ฆฌ์ฆˆ 1ํƒ„์ธ RecyclerView ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์†Œ๊ฐœํ•ด๋ณด๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค! RecyclerView ๋ž€?! RecyclerView๋ž€ ? ๋ฐ์ดํ„ฐ ์ง‘ํ•ฉ๋“ค์„ ๊ฐ๊ฐ์˜ ๊ฐœ๋ณ„ ์•„์ดํ…œ ๋‹จ์œ„๋กœ ๊ตฌ์„ฑํ•˜์—ฌ ํ™”๋ฉด์— ์ถœ๋ ฅํ•ด

yunaaaas.tistory.com

 

 

[Android/Kotlin] RecyclerView ํด๋ฆญ ์ด๋ฒคํŠธ ์ ์šฉํ•˜๊ธฐ

RecyclerView ์ฒซ๋ฒˆ์งธ ์‹œ๋ฆฌ์ฆˆ์— ์ด์–ด ๋‘๋ฒˆ์งธ ์‹œ๋ฆฌ์ฆˆ์ธ ํด๋ฆญ ๋ฆฌ์Šค๋„ˆ๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ ๋ฆฌ์‚ฌ์ดํด๋Ÿฌ๋ทฐ ์•„์ดํ…œ์— ๊ฐ๊ฐ ํด๋ฆญ ์ด๋ฒคํŠธ๋ฅผ ์ ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์ ์–ด๋ณด๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค : ) RecyclerView ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€

yunaaaas.tistory.com

 

 

์˜ค๋Š˜์€ ์•„๋ž˜์™€ ๊ฐ™์ด AnimationTransition์„ ํ™œ์šฉํ•˜์—ฌ RecyclerView Item์— Transition์„ ์ ์šฉํ•ด๋ณด๋Š” ๊ฒƒ์— ๋Œ€ํ•ด ํฌ์ŠคํŒ…ํ•ด๋ณผ๊ฒŒ์š”~

 

 


๐Ÿ“Œ RecyclerView Item, ProfileDetailActivity xml์— android:transitionName ์ถ”๊ฐ€ํ•ด์ฃผ๊ธฐ!

Animation Transition์„ ์‚ฌ์šฉํ•˜์‹œ๋ ค๋ฉด ?!

style์— <item name="android:windowActivityTransitions">true</item> ๊ฐ€ ์„ค์ •๋˜์–ด์žˆ์–ด์•ผ ํ•œ๋‹ค๋Š” ์  ์ฃผ์˜ํ•ด์ฃผ์„ธ์š”!

 

item_recycler_ex.xml 

Profile ์‚ฌ์ง„๊ณผ name๋งŒ ํ™”๋ฉด์ „ํ™˜์—์„œ ํ•„์š”ํ•˜๋ฏ€๋กœ ๋‘๊ตฐ๋ฐ์—๋งŒ andorid:transitionName์„ ์ถ”๊ฐ€ํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:tools="http://schemas.android.com/tools"
    android:paddingHorizontal="20dp"
    android:paddingVertical="10dp"
    android:background="@color/purple_500"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <ImageView
        android:id="@+id/img_rv_photo"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@color/black"
        android:transitionName="image"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>

    <TextView
        android:id="@+id/tv_rv_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="์ด๋ฆ„"
        android:transitionName="name"
        android:textStyle="bold"
        android:layout_marginLeft="20dp"
        android:textSize="24sp"
        android:textColor="@color/white"
        app:layout_constraintLeft_toRightOf="@id/img_rv_photo"
        app:layout_constraintTop_toTopOf="parent"/>

    <TextView
        android:id="@+id/tv_rv_age"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="๋‚˜์ด"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="20dp"
        android:textSize="18sp"
        android:textColor="@color/white"
        app:layout_constraintLeft_toRightOf="@id/img_rv_photo"
        app:layout_constraintTop_toBottomOf="@id/tv_rv_name"/>


</androidx.constraintlayout.widget.ConstraintLayout>

 

activity_profile_detail.xml

์œ„์™€ ๋™์ผํ•œ ์ด๋ฆ„์œผ๋กœ transtionName์„ ์ง€์ •ํ•ด์ฃผ์–ด์•ผ ์ œ๋Œ€๋กœ ํ™”๋ฉด Animation์ด ์ด๋ค„์ง‘๋‹ˆ๋‹ค!

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ProfileDetailActivity">

    <ImageView
        android:id="@+id/img_profile"
        android:layout_width="200dp"
        android:layout_height="0dp"
        android:layout_marginTop="20dp"
        android:transitionName="image"
        app:layout_constraintDimensionRatio="1:1"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"/>

    <TextView
        android:id="@+id/tv_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="25sp"
        android:textColor="@color/black"
        android:layout_marginTop="20dp"
        android:transitionName="name"
        app:layout_constraintTop_toBottomOf="@id/img_profile"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"/>


</androidx.constraintlayout.widget.ConstraintLayout>

 

 

๐Ÿ“Œ ItemClickListener ์™ธ๋ถ€์— ๋งŒ๋“ค๊ธฐ

RecyclerView ClickEvent ์ ์šฉํ•˜๊ธฐ 2๋ฒˆ์งธ ๋ฐฉ๋ฒ•์—์„œ ์™ธ๋ถ€์— ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•์„ ์†Œ๊ฐœํ•˜์˜€์œผ๋‹ˆ ์ƒ๋žตํ•˜๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

๊ธฐ์กด ์•„์ดํ…œ ํด๋ฆญ ์ด๋ฒคํŠธ๋Š” ViewHodler์—์„œ ๊ฐ„๋‹จํ•˜๊ฒŒ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์ง€๋งŒ ActivityOptions.makeSceneTransitionAnimation()์—๋Š” ์•กํ‹ฐ๋น„ํ‹ฐ๊ฐ€ ํ•„์š”ํ•œ๋ฐ ViewHolder์—๋Š” ์•กํ‹ฐ๋น„ํ‹ฐ๋ฅผ ๋ถˆ๋Ÿฌ์˜ฌ ์ˆ˜ ์—†์œผ๋ฏ€๋กœ ์•„์ดํ…œ ํด๋ฆญ๋ฆฌ์Šค๋„ˆ๋ฅผ Activity์—์„œ ์ž‘์„ฑํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 

makeSceneTransitionAnimation์„ ์ ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์ „์— ํฌ์ŠคํŒ…ํ–ˆ๋˜ ๋‚ด์šฉ๊ณผ ๋™์ผํ•˜๊ฒŒ ์ ์šฉํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค !

 

[Android/Kotlin] ๊ฐ„๋‹จํ•œ Animation Transition ๊ตฌํ˜„ํ•˜๊ธฐ

์š”์ฆ˜ ๋‹ค์–‘ํ•œ ์ธํ„ฐ๋ ‰์…˜์— ๋Œ€ํ•ด ๊ด€์‹ฌ์ด ์ƒ๊ฒจ ์ฐพ์•„๋ณด๋˜ ๋„์ค‘ ๋‹ค์Œ๊ณผ ๊ฐ™์€ Animation์„ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์•Œ๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค ! AcitivityOptions์˜ makeScenceTransitionAnimation() ์„ ์ด์šฉํ•˜์—ฌ Animation Transition..

yunaaaas.tistory.com

 MainActivity.kt

ClickListener์— ActivityOptions๋ฅผ ๋งŒ๋“ค์–ด์ฃผ์–ด Intent ์ „๋‹ฌ์‹œ์— ๋„ฃ์–ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

๋‹จ LOLLIPOP ๋ฒ„์ „์ธ API 21 ์ด์ƒ๋ถ€ํ„ฐ๋งŒ ์• ๋‹ˆ๋ฉ”์ด์…˜ ํ˜ธํ™˜์ด ๊ฐ€๋Šฅํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์•„๋ž˜์™€ ๊ฐ™์ด if ๋ฌธ์œผ๋กœ ๋ถ„๊ธฐ์ฒ˜๋ฆฌ๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.

   profileAdapter.setOnItemClickListener(object : ProfileAdapter.OnItemClickListener{
            override fun onItemClick(v: View, data: ProfileData, pos : Int) {
                Intent(this@MainActivity, ProfileDetailActivity::class.java).apply {
                    putExtra("data", data)
                    addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                }.run {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        val Pair1 = Pair.create<View,String>(v.img_rv_photo,"image")
                        val Pair2 = Pair.create<View,String>(v.tv_rv_name,"name")
                        val options : ActivityOptions = ActivityOptions.makeSceneTransitionAnimation(this@MainActivity, Pair1, Pair2)
                        startActivity(this,options.toBundle())
                    } else {
                        startActivity(this)
                    }
                }
            }

        })

 

๋ฐ˜์‘ํ˜•