Skip to content
This repository was archived by the owner on Nov 21, 2019. It is now read-only.

Commit ad731db

Browse files
committed
More cleanups:
- Remove unused lines - Replace logging to sys.out to Log - Use val instead of var where appropriate - Use `layout.findViewById(R.id.title) as TextView` instead of `layout.findViewById<TextView>(R.id.title)` because the latter could lead to unchecked nullability issues. - Remove the unused ViewModel - Remove "+" in "Constraint" properties in the MotionScene XML files because these should be defined in the original layout XML and duplicate definitions of id are warned by IDE
1 parent ab88f4c commit ad731db

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+150
-235
lines changed

motionlayout/src/main/java/com/google/androidstudio/motionlayoutexample/DemoActivity.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,23 @@
1616

1717
package com.google.androidstudio.motionlayoutexample
1818

19+
import android.os.Build
1920
import android.os.Bundle
21+
import android.support.annotation.RequiresApi
2022
import android.support.constraint.motion.MotionLayout
2123
import android.support.v7.app.AppCompatActivity
2224
import android.view.View
2325
import android.widget.ImageView
2426

27+
@RequiresApi(Build.VERSION_CODES.LOLLIPOP) // for View#clipToOutline
2528
class DemoActivity : AppCompatActivity() {
2629
var motionLayout : View? = null
2730

2831
override fun onCreate(savedInstanceState: Bundle?) {
2932
super.onCreate(savedInstanceState)
3033
val layout = intent.getIntExtra("layout_file_id", R.layout.motion_01_basic)
3134
setContentView(layout)
32-
motionLayout = findViewById<View>(R.id.motionLayout)
35+
motionLayout = findViewById(R.id.motionLayout)
3336

3437
if (layout == R.layout.motion_11_coordinatorlayout) {
3538
val icon = findViewById<ImageView>(R.id.icon)

motionlayout/src/main/java/com/google/androidstudio/motionlayoutexample/DemosAdapter.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,20 @@ class DemosAdapter(private val myDataset: Array<DemosAdapter.Demo>) :
2626
RecyclerView.Adapter<DemosAdapter.ViewHolder>() {
2727

2828
data class Demo(val title: String, val description : String, val layout : Int = 0, val activity : Class<*> = DemoActivity::class.java) {
29-
constructor(title: String, description: String, activity : Class<*> = DemoActivity::class.java) : this(title, description, 0, activity) {}
29+
constructor(title: String, description: String, activity : Class<*> = DemoActivity::class.java) : this(title, description, 0, activity)
3030
}
3131

3232
class ViewHolder(val layout: ConstraintLayout) : RecyclerView.ViewHolder(layout) {
33-
var title = layout.findViewById<TextView>(R.id.title)
34-
var description = layout.findViewById<TextView>(R.id.description)
33+
var title = layout.findViewById(R.id.title) as TextView
34+
var description = layout.findViewById(R.id.description) as TextView
3535
var layoutFileId = 0
3636
var activity : Class<*>? = null
3737

3838
init {
3939
layout.setOnClickListener {
4040
val context = it?.context as MainActivity
41-
if (activity != null) {
42-
context.start(activity!!, layoutFileId)
41+
activity?.let {
42+
context.start(it, layoutFileId)
4343
}
4444
}
4545
}

motionlayout/src/main/java/com/google/androidstudio/motionlayoutexample/MainActivity.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class MainActivity : AppCompatActivity(), CompoundButton.OnCheckedChangeListener
1616
private lateinit var recyclerView: RecyclerView
1717
private lateinit var viewAdapter: RecyclerView.Adapter<*>
1818
private lateinit var viewManager: RecyclerView.LayoutManager
19-
var doShowPaths = false
19+
private var doShowPaths = false
2020

2121
private val myDataset: Array<DemosAdapter.Demo> = arrayOf(
2222
DemosAdapter.Demo("Basic Example (1/2)", "Basic motion example using referenced ConstraintLayout files", R.layout.motion_01_basic),

motionlayout/src/main/java/com/google/androidstudio/motionlayoutexample/fragmentsdemo/CustomAdapter.kt

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,31 +23,25 @@ import android.support.v7.widget.RecyclerView
2323
import android.view.LayoutInflater
2424
import android.view.View
2525
import android.view.ViewGroup
26-
import android.widget.FrameLayout
2726
import android.widget.TextView
2827
import com.google.androidstudio.motionlayoutexample.R
2928

30-
class CustomAdapter(val userList: ArrayList<User>): RecyclerView.Adapter<CustomAdapter.ViewHolder>() {
29+
class CustomAdapter(private val userList: ArrayList<User>): RecyclerView.Adapter<CustomAdapter.ViewHolder>() {
3130

3231
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
33-
holder?.txtName?.text = userList[position].name
34-
holder?.txtTitle?.text = userList[position].title
35-
holder?.itemView.setOnClickListener({
36-
var parent = it?.parent?.parent?.parent?.parent
32+
holder.txtName.text = userList[position].name
33+
holder.txtTitle.text = userList[position].title
34+
holder.itemView.setOnClickListener({
35+
val parent = it?.parent?.parent?.parent?.parent
3736
if (parent is MotionLayout) {
3837
val offsetViewBounds = Rect()
3938
it.getDrawingRect(offsetViewBounds)
4039
parent.offsetDescendantRectToMyCoords(it, offsetViewBounds)
41-
var placeholder = parent.findViewById<FrameLayout>(R.id.rv_item_placeholder)
42-
// placeholder.layout(offsetViewBounds.left, offsetViewBounds.top,
43-
// offsetViewBounds.right, offsetViewBounds.bottom)
44-
var transaction = (it.context as AppCompatActivity).supportFragmentManager.beginTransaction()
45-
var fragment = ItemFragment.newInstance()
40+
val transaction = (it.context as AppCompatActivity).supportFragmentManager.beginTransaction()
41+
val fragment = ItemFragment.newInstance()
4642
fragment.update(holder)
4743
transaction.replace(R.id.rv_item_placeholder, fragment)
4844
transaction.commitNow()
49-
// holder.itemView.visibility = View.INVISIBLE
50-
// parent.updateFrame(placeholder, offsetViewBounds, View.VISIBLE)
5145
parent.transitionToEnd()
5246
}
5347
})
@@ -63,8 +57,8 @@ class CustomAdapter(val userList: ArrayList<User>): RecyclerView.Adapter<CustomA
6357
}
6458

6559
class ViewHolder(itemView: View): RecyclerView.ViewHolder(itemView){
66-
val txtName = itemView.findViewById<TextView>(R.id.txtName)
67-
val txtTitle = itemView.findViewById<TextView>(R.id.txtTitle)
60+
val txtName = itemView.findViewById(R.id.txtName) as TextView
61+
val txtTitle = itemView.findViewById(R.id.txtTitle) as TextView
6862
}
6963

7064
}

motionlayout/src/main/java/com/google/androidstudio/motionlayoutexample/fragmentsdemo/FragmentExample2Activity.kt

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,21 @@ class FragmentExample2Activity : AppCompatActivity(), View.OnClickListener, Moti
2828
override fun onTransitionChange(p0: MotionLayout?, p1: Int, p2: Int, progress: Float) {
2929
if (progress - lastProgress > 0) {
3030
// from start to end
31-
var atEnd = Math.abs(progress - 1f) < 0.1f
31+
val atEnd = Math.abs(progress - 1f) < 0.1f
3232
if (atEnd && fragment is MainFragment) {
33-
var transaction = supportFragmentManager.beginTransaction()
33+
val transaction = supportFragmentManager.beginTransaction()
3434
transaction
3535
.setCustomAnimations(R.animator.show, 0)
3636
fragment = ListFragment.newInstance()
37-
// fragment = SecondFragment.newInstance()
38-
// (fragment as SecondFragment).setEnterTransition((fragment as SecondFragment).view)
3937
transaction
4038
.replace(R.id.container, fragment)
4139
.commitNow()
4240
}
4341
} else {
4442
// from end to start
45-
var atStart = progress < 0.9f
43+
val atStart = progress < 0.9f
4644
if (atStart && fragment is ListFragment) {
47-
var transaction = supportFragmentManager.beginTransaction()
45+
val transaction = supportFragmentManager.beginTransaction()
4846
transaction
4947
.setCustomAnimations(0, R.animator.hide)
5048
fragment = MainFragment.newInstance()
@@ -80,7 +78,7 @@ class FragmentExample2Activity : AppCompatActivity(), View.OnClickListener, Moti
8078

8179
override fun onClick(view: View?) {
8280
if (view?.id == R.id.toggle) {
83-
var transaction = supportFragmentManager.beginTransaction()
81+
val transaction = supportFragmentManager.beginTransaction()
8482
fragment = if (fragment == null || fragment is MainFragment) {
8583
last = 1f
8684
transaction

motionlayout/src/main/java/com/google/androidstudio/motionlayoutexample/fragmentsdemo/FragmentExampleActivity.kt

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,22 @@ class FragmentExampleActivity : AppCompatActivity(), View.OnClickListener, Motio
2828
override fun onTransitionChange(p0: MotionLayout?, p1: Int, p2: Int, progress: Float) {
2929
if (progress - lastProgress > 0) {
3030
// from start to end
31-
var atEnd = Math.abs(progress - 1f) < 0.1f
31+
val atEnd = Math.abs(progress - 1f) < 0.1f
3232
if (atEnd && fragment is MainFragment) {
33-
var transaction = supportFragmentManager.beginTransaction()
33+
val transaction = supportFragmentManager.beginTransaction()
3434
transaction
3535
.setCustomAnimations(R.animator.show, 0)
36-
// fragment = ListFragment.newInstance()
3736
fragment = SecondFragment.newInstance()
38-
// (fragment as SecondFragment).setEnterTransition((fragment as SecondFragment).view)
3937
transaction
4038
.setCustomAnimations(R.animator.show, 0)
4139
.replace(R.id.container, fragment)
4240
.commitNow()
4341
}
4442
} else {
4543
// from end to start
46-
var atStart = progress < 0.9f
44+
val atStart = progress < 0.9f
4745
if (atStart && fragment is SecondFragment) {
48-
var transaction = supportFragmentManager.beginTransaction()
46+
val transaction = supportFragmentManager.beginTransaction()
4947
transaction
5048
.setCustomAnimations(0, R.animator.hide)
5149
fragment = MainFragment.newInstance()
@@ -81,7 +79,7 @@ class FragmentExampleActivity : AppCompatActivity(), View.OnClickListener, Motio
8179

8280
override fun onClick(view: View?) {
8381
if (view?.id == R.id.toggle) {
84-
var transaction = supportFragmentManager.beginTransaction()
82+
val transaction = supportFragmentManager.beginTransaction()
8583
fragment = if (fragment == null || fragment is MainFragment) {
8684
last = 1f
8785
transaction

motionlayout/src/main/java/com/google/androidstudio/motionlayoutexample/fragmentsdemo/ItemFragment.kt

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package com.google.androidstudio.motionlayoutexample.fragmentsdemo
1818

19-
import android.arch.lifecycle.ViewModelProviders
2019
import android.os.Bundle
2120
import android.support.v4.app.Fragment
2221
import android.view.LayoutInflater
@@ -31,19 +30,11 @@ class ItemFragment : Fragment() {
3130
fun newInstance() = ItemFragment()
3231
}
3332

34-
private lateinit var viewModel: MainViewModel
35-
3633
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
3734
savedInstanceState: Bundle?): View {
3835
return inflater.inflate(R.layout.item_layout, container, false)
3936
}
4037

41-
override fun onActivityCreated(savedInstanceState: Bundle?) {
42-
super.onActivityCreated(savedInstanceState)
43-
viewModel = ViewModelProviders.of(this).get(MainViewModel::class.java)
44-
// TODO: Use the ViewModel
45-
}
46-
4738
private lateinit var myHolder: CustomAdapter.ViewHolder
4839

4940
fun update(holder: CustomAdapter.ViewHolder) {
@@ -54,7 +45,7 @@ class ItemFragment : Fragment() {
5445

5546
override fun onStart() {
5647
super.onStart()
57-
if (myHolder != null) {
48+
if (this::myHolder.isInitialized) {
5849
update(myHolder)
5950
}
6051
}

motionlayout/src/main/java/com/google/androidstudio/motionlayoutexample/fragmentsdemo/ListFragment.kt

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616

1717
package com.google.androidstudio.motionlayoutexample.fragmentsdemo
1818

19-
import android.arch.lifecycle.ViewModelProviders
2019
import android.os.Bundle
2120
import android.support.v4.app.Fragment
2221
import android.support.v7.widget.LinearLayoutManager
2322
import android.support.v7.widget.RecyclerView
23+
import android.util.Log
2424
import android.view.LayoutInflater
2525
import android.view.View
2626
import android.view.ViewGroup
@@ -35,23 +35,14 @@ class ListFragment : Fragment() {
3535
fun newInstance() = ListFragment()
3636
}
3737

38-
private lateinit var viewModel: MainViewModel
39-
4038
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
4139
savedInstanceState: Bundle?): View {
42-
System.out.println("onCreateView, container is " + container)
40+
Log.i(ListFragment::class.java.simpleName, "onCreateView, container is $container")
4341
return inflater.inflate(R.layout.motion_22_list_fragment, container, false)
4442
}
4543

46-
override fun onActivityCreated(savedInstanceState: Bundle?) {
47-
super.onActivityCreated(savedInstanceState)
48-
viewModel = ViewModelProviders.of(this).get(MainViewModel::class.java)
49-
// TODO: Use the ViewModel
50-
}
51-
5244
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
53-
// motionLayout = view.findViewById(R.id.main)
54-
recyclerView = view.findViewById<RecyclerView>(R.id.list)
45+
recyclerView = view.findViewById(R.id.list)
5546

5647
recyclerView.layoutManager = LinearLayoutManager(
5748
context, LinearLayout.VERTICAL, false)
@@ -73,7 +64,7 @@ class ListFragment : Fragment() {
7364
users.add(User("John", "Dr"))
7465
users.add(User("Amy", "Mrs"))
7566

76-
var adapter = CustomAdapter(users)
67+
val adapter = CustomAdapter(users)
7768
recyclerView.adapter = adapter
7869

7970
super.onViewCreated(view, savedInstanceState)

motionlayout/src/main/java/com/google/androidstudio/motionlayoutexample/fragmentsdemo/MainFragment.kt

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package com.google.androidstudio.motionlayoutexample.fragmentsdemo
1818

19-
import android.arch.lifecycle.ViewModelProviders
2019
import android.os.Bundle
2120
import android.support.v4.app.Fragment
2221
import android.view.LayoutInflater
@@ -30,17 +29,8 @@ class MainFragment : Fragment() {
3029
fun newInstance() = MainFragment()
3130
}
3231

33-
private lateinit var viewModel: MainViewModel
34-
3532
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
3633
savedInstanceState: Bundle?): View {
3734
return inflater.inflate(R.layout.motion_21_main_fragment, container, false)
3835
}
39-
40-
override fun onActivityCreated(savedInstanceState: Bundle?) {
41-
super.onActivityCreated(savedInstanceState)
42-
viewModel = ViewModelProviders.of(this).get(MainViewModel::class.java)
43-
// TODO: Use the ViewModel
44-
}
45-
4636
}

motionlayout/src/main/java/com/google/androidstudio/motionlayoutexample/fragmentsdemo/MainViewModel.kt

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)