Skip to content

Commit 9a43b01

Browse files
add custom transitions
1 parent f136384 commit 9a43b01

25 files changed

+525
-159
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ of ***Transition*** start and end point to same value.
318318

319319
#### Note:
320320
🔥🔥🔥 In tutorial 2-4 and 2-5, having same background color for both fragments causing second fragment's enter transition(CircularReveal and Slide.BOTTOM) to not work
321-
So when using **Transitions** that extend ```Visiblity``` class such as Slide, or Fade be careful about background color
321+
So when using **Transitions** that extends ```Visiblity``` class such as Slide, or Fade be careful about background color
322322
Either set callback and set start and end properties for scene with
323323

324324
```

Tutorial3-1Transitions/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ dependencies {
4747

4848
implementation 'androidx.appcompat:appcompat:1.2.0'
4949
implementation 'com.google.android.material:material:1.2.1'
50-
implementation 'androidx.constraintlayout:constraintlayout:2.0.2'
50+
implementation 'androidx.constraintlayout:constraintlayout:2.0.3'
5151
implementation 'androidx.viewpager2:viewpager2:1.0.0'
5252
implementation 'androidx.recyclerview:recyclerview:1.2.0-alpha06'
5353
implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.1.0'

Tutorial3-1Transitions/src/main/java/com/smarttoolfactory/tutorial3_1transitions/adapter/model/MagazineModel.kt

+3-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ data class MagazineModel(
99
@DrawableRes val drawableRes: Int,
1010
val title: String,
1111
val body: String,
12-
val transitionId: Int = 0
12+
val transitionId:Int,
13+
val transitionName: String = ""
1314
) : Parcelable {
14-
var transitionName = "tr$drawableRes$transitionId"
15+
1516
}

Tutorial3-1Transitions/src/main/java/com/smarttoolfactory/tutorial3_1transitions/adapter/viewholder/MagazineViewBinder.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class MagazineViewHolder(
5050

5151
fun bind(model: MagazineModel) {
5252

53-
// 🔥 Set transition name to resource to drawable
53+
// Set transition name, resource drawable and title for magazine
5454
binding.ivMagazineCover.transitionName = model.transitionName
5555
setImageUrl(binding.ivMagazineCover, model.drawableRes)
5656

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package com.smarttoolfactory.tutorial3_1transitions.adapter.viewholder
2+
3+
import android.view.ViewGroup
4+
import android.widget.ImageView
5+
import androidx.recyclerview.widget.RecyclerView
6+
import com.bumptech.glide.Glide
7+
import com.bumptech.glide.request.RequestOptions
8+
import com.smarttoolfactory.tutorial3_1transitions.R
9+
import com.smarttoolfactory.tutorial3_1transitions.adapter.model.PostCardModel
10+
import com.smarttoolfactory.tutorial3_1transitions.databinding.ItemPostGridBinding
11+
12+
/**
13+
* ViewBinder for Activity1_2
14+
*/
15+
class PostGridViewBinder(
16+
private val onItemClick: ((ItemPostGridBinding, PostCardModel) -> Unit)? = null
17+
) : MappableItemViewBinder<PostCardModel, PostGridViewHolder>(PostCardModel::class.java) {
18+
19+
override fun createViewHolder(parent: ViewGroup): RecyclerView.ViewHolder {
20+
return PostGridViewHolder(
21+
parent.inflate(getItemLayoutResource()),
22+
onItemClick
23+
)
24+
}
25+
26+
override fun bindViewHolder(model: PostCardModel, viewHolder: PostGridViewHolder) {
27+
viewHolder.bind(model)
28+
}
29+
30+
override fun getItemLayoutResource(): Int {
31+
return R.layout.item_post_grid
32+
}
33+
34+
override fun areItemsTheSame(oldItem: PostCardModel, newItem: PostCardModel): Boolean {
35+
return oldItem.post == newItem.post
36+
}
37+
38+
override fun areContentsTheSame(oldItem: PostCardModel, newItem: PostCardModel): Boolean {
39+
return oldItem == newItem
40+
}
41+
}
42+
43+
class PostGridViewHolder(
44+
private val binding: ItemPostGridBinding,
45+
private val onItemClick: ((ItemPostGridBinding, PostCardModel) -> Unit)? = null
46+
) :
47+
RecyclerView.ViewHolder(binding.root) {
48+
49+
fun bind(model: PostCardModel) {
50+
51+
val post = model.post
52+
binding.tvTitle.text = post.title
53+
54+
setImageUrl(binding.ivPhoto, model.drawableRes)
55+
56+
binding.root.setOnClickListener {
57+
onItemClick?.invoke(binding, model)
58+
}
59+
}
60+
61+
private fun setImageUrl(view: ImageView, drawableRes: Int) {
62+
63+
try {
64+
65+
val requestOptions = RequestOptions()
66+
requestOptions.placeholder(R.drawable.ic_launcher_background)
67+
68+
Glide
69+
.with(view.context)
70+
.setDefaultRequestOptions(requestOptions)
71+
.load(drawableRes)
72+
.into(view)
73+
74+
} catch (e: Exception) {
75+
e.printStackTrace()
76+
}
77+
}
78+
79+
}

Tutorial3-1Transitions/src/main/java/com/smarttoolfactory/tutorial3_1transitions/chapter1_activity_transitions/Activity1_0CustomTransitions.kt

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ package com.smarttoolfactory.tutorial3_1transitions.chapter1_activity_transition
22

33
import android.graphics.Color
44
import android.os.Bundle
5-
import androidx.transition.*
65
import android.view.View
76
import android.widget.Button
87
import android.widget.TextView
98
import android.widget.Toast
109
import androidx.appcompat.app.AppCompatActivity
1110
import androidx.constraintlayout.widget.ConstraintLayout
11+
import androidx.transition.Transition
12+
import androidx.transition.TransitionManager
13+
import androidx.transition.TransitionSet
1214
import com.smarttoolfactory.tutorial3_1transitions.R
1315
import com.smarttoolfactory.tutorial3_1transitions.transition.*
1416

@@ -152,7 +154,7 @@ class Activity1_0CustomTransitions : AppCompatActivity() {
152154
val transitions = TransitionSet()
153155

154156
val transition =
155-
CustomBackgroundTransition(
157+
CustomBackgroundColorTransition(
156158
Color.YELLOW,
157159
Color.MAGENTA,
158160
true

Tutorial3-1Transitions/src/main/java/com/smarttoolfactory/tutorial3_1transitions/chapter1_activity_transitions/Activity1_3RecyclerViewToViewPager2Transition.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ open class Activity1_3RecyclerViewToViewPager2Transition : AppCompatActivity() {
9090
}
9191

9292
/*
93-
TODO This is important for setting position of RV and getting imageView in that position
94-
to set enter transition after going back from detail Activity
93+
TODO It is required to set position of RV and getting imageView in that position
94+
to set enter transition after coming back from detail Activity
9595
*/
9696
override fun onActivityReenter(resultCode: Int, data: Intent?) {
9797
super.onActivityReenter(resultCode, data)

Tutorial3-1Transitions/src/main/java/com/smarttoolfactory/tutorial3_1transitions/chapter2_fragment_transitions/Fragment2_4MagazineList.kt

+10-10
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@ import android.os.Bundle
44
import android.view.LayoutInflater
55
import android.view.View
66
import android.view.ViewGroup
7-
import androidx.constraintlayout.widget.ConstraintLayout
87
import androidx.core.view.doOnPreDraw
98
import androidx.fragment.app.Fragment
109
import androidx.navigation.NavDirections
1110
import androidx.navigation.fragment.FragmentNavigatorExtras
1211
import androidx.navigation.fragment.findNavController
1312
import androidx.recyclerview.widget.RecyclerView
1413
import androidx.transition.Fade
15-
import com.google.android.material.appbar.AppBarLayout
1614
import com.smarttoolfactory.tutorial3_1transitions.ImageData
1715
import com.smarttoolfactory.tutorial3_1transitions.R
1816
import com.smarttoolfactory.tutorial3_1transitions.adapter.SingleViewBinderListAdapter
@@ -46,10 +44,6 @@ class Fragment2_4MagazineList : Fragment() {
4644

4745
private fun prepareTransitions(view: View) {
4846

49-
val appBarLayout: AppBarLayout = view.findViewById(R.id.appbar)
50-
val constraintLayout: ConstraintLayout = view.findViewById(R.id.constraintLayout)
51-
52-
5347
/*
5448
🔥🔥 Setting allowReturnTransitionOverlap to false lets this fragment's
5549
reenterTransition to wait previous fragment's returnTransition to finish
@@ -121,14 +115,20 @@ class Fragment2_4MagazineList : Fragment() {
121115
findNavController().navigate(direction, extras)
122116
}
123117

124-
private fun getMagazineList(): ArrayList<MagazineModel> {
118+
private fun getMagazineList(id: Int = 0): ArrayList<MagazineModel> {
125119

126120
val magazineList = ArrayList<MagazineModel>()
121+
127122
repeat(ImageData.MAGAZINE_DRAWABLES.size) {
123+
124+
val transitionName = "#tr$id-${ImageData.MAGAZINE_DRAWABLES[it]}"
125+
128126
val magazineModel = MagazineModel(
129-
ImageData.MAGAZINE_DRAWABLES[it],
130-
"Issue #${ImageData.MAGAZINE_DRAWABLES[it]}",
131-
""
127+
drawableRes = ImageData.MAGAZINE_DRAWABLES[it],
128+
title = "$id-${ImageData.MAGAZINE_DRAWABLES[it]}",
129+
body = "",
130+
transitionId = id,
131+
transitionName = transitionName
132132
)
133133
magazineList.add(magazineModel)
134134
}

Tutorial3-1Transitions/src/main/java/com/smarttoolfactory/tutorial3_1transitions/chapter2_fragment_transitions/Fragment2_5MagazineDetailAlt.kt

+4-4
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ class Fragment2_5MagazineDetailAlt : Fragment() {
136136

137137
private fun createEnterTransition(view: View): Transition {
138138

139-
val tvBody = view.findViewById<View>(R.id.recyclerView)
139+
val recyclerView = view.findViewById<View>(R.id.recyclerView)
140140
val viewImageBackground: View = view.findViewById(R.id.viewImageBackground)
141141

142142

@@ -151,7 +151,7 @@ class Fragment2_5MagazineDetailAlt : Fragment() {
151151
startDelay = 400
152152
duration = 600
153153
excludeTarget(viewImageBackground, true)
154-
addTarget(tvBody)
154+
addTarget(recyclerView)
155155
}
156156

157157

@@ -177,7 +177,7 @@ class Fragment2_5MagazineDetailAlt : Fragment() {
177177
private fun createReturnTransition(view: View): Transition {
178178

179179
val viewTop = view.findViewById<View>(R.id.viewImageBackground)
180-
val tvBody = view.findViewById<View>(R.id.recyclerView)
180+
val recyclerView = view.findViewById<View>(R.id.recyclerView)
181181

182182
val transitionSetReturn = TransitionSet()
183183

@@ -197,7 +197,7 @@ class Fragment2_5MagazineDetailAlt : Fragment() {
197197
android.R.interpolator.linear_out_slow_in
198198
)
199199
duration = 900
200-
addTarget(tvBody)
200+
addTarget(recyclerView)
201201
}
202202

203203
transitionSetReturn.addTransition(slideToTop)

0 commit comments

Comments
 (0)