Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate kotlin-android-extension to view Binding at motionLayout #204

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions ConstraintLayoutExamples/motionlayout/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
compileSdkVersion rootProject.compileSdkVersion
defaultConfig {
Expand All @@ -37,6 +35,10 @@ android {
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}

buildFeatures {
viewBinding = true
}
}

dependencies {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import android.widget.CompoundButton
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.androidstudio.motionlayoutexample.databinding.ActivityMainBinding
import com.google.androidstudio.motionlayoutexample.fragmentsdemo.FragmentExample2Activity
import com.google.androidstudio.motionlayoutexample.fragmentsdemo.FragmentExampleActivity
import com.google.androidstudio.motionlayoutexample.histogramdemo.HistogramActivity
import com.google.androidstudio.motionlayoutexample.viewpagerdemo.ViewPagerActivity
import com.google.androidstudio.motionlayoutexample.viewpagerdemo.ViewPagerActivity2
import com.google.androidstudio.motionlayoutexample.youtubedemo.YouTubeDemoActivity
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity(), CompoundButton.OnCheckedChangeListener {
private lateinit var recyclerView: RecyclerView
Expand Down Expand Up @@ -53,16 +53,17 @@ class MainActivity : AppCompatActivity(), CompoundButton.OnCheckedChangeListener

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
viewManager = LinearLayoutManager(this)
viewAdapter = DemosAdapter(dataset)
recyclerView = findViewById<RecyclerView>(R.id.recyclerview).apply {
recyclerView = binding.recyclerview.apply {
setHasFixedSize(true)
layoutManager = viewManager
adapter = viewAdapter
}

showPaths.setOnCheckedChangeListener(this)
binding.showPaths.setOnCheckedChangeListener(this)
}

override fun onCheckedChanged(p0: CompoundButton?, value: Boolean) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,18 @@ package com.google.androidstudio.motionlayoutexample.fragmentsdemo

import android.graphics.Rect
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.constraintlayout.motion.widget.MotionLayout
import androidx.recyclerview.widget.RecyclerView
import com.google.androidstudio.motionlayoutexample.R
import com.google.androidstudio.motionlayoutexample.databinding.ItemLayoutBinding

class CustomAdapter(private val userList: ArrayList<User>): RecyclerView.Adapter<CustomAdapter.ViewHolder>() {
class CustomAdapter(private val userList: ArrayList<User>) : RecyclerView.Adapter<CustomAdapter.ViewHolder>() {

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.txtName.text = userList[position].name
holder.txtTitle.text = userList[position].title
holder.binding.txtName.text = userList[position].name
holder.binding.txtTitle.text = userList[position].title
holder.itemView.setOnClickListener {
val parent = it?.parent?.parent?.parent?.parent
if (parent is MotionLayout) {
Expand All @@ -48,17 +47,14 @@ class CustomAdapter(private val userList: ArrayList<User>): RecyclerView.Adapter
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val v = LayoutInflater.from(parent.context).inflate(R.layout.item_layout, parent, false)
return ViewHolder(v)
val binding = ItemLayoutBinding.inflate(LayoutInflater.from(parent.context), parent, false)
return ViewHolder(binding)
}

override fun getItemCount(): Int {
return userList.size
}

class ViewHolder(itemView: View): RecyclerView.ViewHolder(itemView){
val txtName = itemView.findViewById(R.id.txtName) as TextView
val txtTitle = itemView.findViewById(R.id.txtTitle) as TextView
}
class ViewHolder(val binding: ItemLayoutBinding) : RecyclerView.ViewHolder(binding.root)

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.constraintlayout.motion.widget.MotionLayout
import androidx.constraintlayout.motion.widget.MotionScene
import androidx.fragment.app.Fragment
import com.google.androidstudio.motionlayoutexample.R
import kotlinx.android.synthetic.main.main_activity.*
import com.google.androidstudio.motionlayoutexample.databinding.MainActivityBinding

class FragmentExample2Activity: AppCompatActivity(), View.OnClickListener, MotionLayout.TransitionListener {

Expand Down Expand Up @@ -74,7 +73,8 @@ class FragmentExample2Activity: AppCompatActivity(), View.OnClickListener, Motio

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.main_activity)
val binding = MainActivityBinding.inflate(layoutInflater)
setContentView(binding.root)
if (savedInstanceState == null) {

fragment = MainFragment.newInstance().also {
Expand All @@ -85,7 +85,7 @@ class FragmentExample2Activity: AppCompatActivity(), View.OnClickListener, Motio
}
//toggle.setOnClickListener(this)
//toggleAnimation.setOnClickListener(this)
motionLayout.setTransitionListener(this)
binding.motionLayout.setTransitionListener(this)
}

override fun onClick(view: View?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.constraintlayout.motion.widget.MotionLayout
import androidx.constraintlayout.motion.widget.MotionScene
import androidx.fragment.app.Fragment
import com.google.androidstudio.motionlayoutexample.R
import kotlinx.android.synthetic.main.main_activity.*
import com.google.androidstudio.motionlayoutexample.databinding.MainActivityBinding

class FragmentExampleActivity : AppCompatActivity(), View.OnClickListener, MotionLayout.TransitionListener {

Expand Down Expand Up @@ -74,15 +73,16 @@ class FragmentExampleActivity : AppCompatActivity(), View.OnClickListener, Motio

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.main_activity)
val binding = MainActivityBinding.inflate(layoutInflater)
setContentView(binding.root)
if (savedInstanceState == null) {
fragment = MainFragment.newInstance().also {
supportFragmentManager.beginTransaction()
.replace(R.id.container, it)
.commitNow()
}
}
motionLayout.setTransitionListener(this)
binding.motionLayout.setTransitionListener(this)
}

override fun onClick(view: View?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.Fragment
import com.google.androidstudio.motionlayoutexample.R
import com.google.androidstudio.motionlayoutexample.databinding.ItemLayoutBinding

class ItemFragment : Fragment() {

Expand All @@ -32,15 +33,15 @@ class ItemFragment : Fragment() {

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View {
return inflater.inflate(R.layout.item_layout, container, false)
return ItemLayoutBinding.inflate(inflater, container, false).root
}

private lateinit var myHolder: CustomAdapter.ViewHolder

fun update(holder: CustomAdapter.ViewHolder) {
myHolder = holder
view?.findViewById<TextView>(R.id.txtTitle)?.text = holder.txtTitle.text
view?.findViewById<TextView>(R.id.txtName)?.text = holder.txtName.text
view?.findViewById<TextView>(R.id.txtTitle)?.text = holder.binding.txtTitle.text
view?.findViewById<TextView>(R.id.txtName)?.text = holder.binding.txtName.text
}

override fun onStart() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.androidstudio.motionlayoutexample.R
import com.google.androidstudio.motionlayoutexample.databinding.Motion22ListFragmentBinding

class ListFragment : Fragment() {

Expand All @@ -37,7 +38,7 @@ class ListFragment : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View {
Log.i(ListFragment::class.java.simpleName, "onCreateView, container is $container")
return inflater.inflate(R.layout.motion_22_list_fragment, container, false)
return Motion22ListFragmentBinding.inflate(inflater, container, false).root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,18 @@ import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.constraintlayout.motion.widget.MotionLayout
import androidx.fragment.app.Fragment
import com.google.androidstudio.motionlayoutexample.R
import com.google.androidstudio.motionlayoutexample.databinding.Motion21SecondFragmentBinding

class SecondFragment : Fragment() {

private lateinit var motionLayout: MotionLayout

companion object {
fun newInstance() = SecondFragment()
}

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View {
return inflater.inflate(R.layout.motion_21_second_fragment, container, false)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
motionLayout = view.findViewById(R.id.main)
super.onViewCreated(view, savedInstanceState)
return Motion21SecondFragmentBinding.inflate(inflater, container, false).root
}

override fun onStart() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import androidx.annotation.Nullable
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import com.google.androidstudio.motionlayoutexample.R
import kotlinx.android.synthetic.main.histogram_layout.*
import com.google.androidstudio.motionlayoutexample.databinding.HistogramLayoutBinding
import java.lang.RuntimeException
import java.util.*
import kotlin.collections.ArrayList
Expand All @@ -46,10 +46,13 @@ class HistogramActivity : AppCompatActivity() {

private val animationGuard: HistogramAnimationGuard = HistogramAnimationGuard()

private lateinit var binding: HistogramLayoutBinding

override fun onCreate(@Nullable savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.histogram_layout)
widget = histogram
binding = HistogramLayoutBinding.inflate(layoutInflater)
setContentView(binding.root)
widget = binding.histogram
restoreView(savedInstanceState)
widget!!.setTransitionListener(animationGuard.animationListener)
}
Expand Down Expand Up @@ -126,7 +129,7 @@ class HistogramActivity : AppCompatActivity() {
throw RuntimeException("Restoring array doesn't match the view size.")
}

bars = ArrayList(bars.mapIndexed{ i, metaData ->
bars = ArrayList(bars.mapIndexed { i, metaData ->
HistogramBarMetaData(widget!!.barIds[i], metaData)
})
widget!!.setData(bars)
Expand All @@ -150,8 +153,8 @@ class HistogramActivity : AppCompatActivity() {
* for each bars. It means you cannot use chain feature of constraint layout. You'll need
* to calculate the after-sorted x location of each bars manually and animate them.
*/
sort.setEnabledAndChangeColor(!animationInterruptible)
both.setEnabledAndChangeColor(!animationInterruptible)
binding.sort.setEnabledAndChangeColor(!animationInterruptible)
binding.both.setEnabledAndChangeColor(!animationInterruptible)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,24 @@ import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.constraintlayout.motion.widget.MotionLayout
import com.google.androidstudio.motionlayoutexample.R
import com.google.androidstudio.motionlayoutexample.utils.ViewpagerHeader
import kotlinx.android.synthetic.main.motion_16_viewpager.*
import com.google.androidstudio.motionlayoutexample.databinding.Motion16ViewpagerBinding

class ViewPagerActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val layout = R.layout.motion_16_viewpager
setContentView(layout)
val viewPagerHeader = findViewById<ViewpagerHeader>(R.id.motionLayout)
val binding = Motion16ViewpagerBinding.inflate(layoutInflater)
setContentView(binding.root)
val viewPagerHeader = binding.include.motionLayout

val adapter = ViewPagerAdapter(supportFragmentManager)
adapter.addPage("Page 1", R.layout.motion_16_viewpager_page1)
adapter.addPage("Page 2", R.layout.motion_16_viewpager_page2)
adapter.addPage("Page 3", R.layout.motion_16_viewpager_page3)
pager.adapter = adapter
tabs.setupWithViewPager(pager)
binding.pager.adapter = adapter
binding.tabs.setupWithViewPager(binding.pager)
if (viewPagerHeader != null) {
pager.addOnPageChangeListener(viewPagerHeader)
binding.pager.addOnPageChangeListener(viewPagerHeader)
}

val debugMode = if (intent.getBooleanExtra("showPaths", false)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,23 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.constraintlayout.motion.widget.MotionLayout
import androidx.viewpager.widget.ViewPager
import com.google.androidstudio.motionlayoutexample.R
import kotlinx.android.synthetic.main.motion_16_viewpager.*
import com.google.androidstudio.motionlayoutexample.databinding.Motion23ViewpagerBinding

class ViewPagerActivity2 : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val layout = R.layout.motion_23_viewpager
setContentView(layout)
val motionLayout = findViewById<MotionLayout>(R.id.motionLayout)
val binding = Motion23ViewpagerBinding.inflate(layoutInflater)
setContentView(binding.root)
val motionLayout = binding.include.motionLayout

val adapter = ViewPagerAdapter(supportFragmentManager)
adapter.addPage("Page 1", R.layout.motion_16_viewpager_page1)
adapter.addPage("Page 2", R.layout.motion_16_viewpager_page2)
adapter.addPage("Page 3", R.layout.motion_16_viewpager_page3)
pager.adapter = adapter
tabs.setupWithViewPager(pager)
if (motionLayout != null) {
pager.addOnPageChangeListener(motionLayout as ViewPager.OnPageChangeListener)
}
binding.pager.adapter = adapter
binding.tabs.setupWithViewPager(binding.pager)
binding.pager.addOnPageChangeListener(motionLayout as ViewPager.OnPageChangeListener)

val debugMode = if (intent.getBooleanExtra("showPaths", false)) {
MotionLayout.DEBUG_SHOW_PATH
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,15 @@ import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.constraintlayout.motion.widget.MotionLayout
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.androidstudio.motionlayoutexample.R
import com.google.androidstudio.motionlayoutexample.databinding.Motion24YoutubeBinding

class YouTubeDemoActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.motion_24_youtube)
val motionLayout = findViewById<MotionLayout>(R.id.motionLayout).apply {
savedInstanceState
}
findViewById<RecyclerView>(R.id.recyclerview_front).apply {
val binding = Motion24YoutubeBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.recyclerviewFront.apply {
adapter = FrontPhotosAdapter()
isNestedScrollingEnabled = false
layoutManager = LinearLayoutManager(this@YouTubeDemoActivity)
Expand All @@ -41,6 +38,6 @@ class YouTubeDemoActivity : AppCompatActivity() {
} else {
MotionLayout.DEBUG_SHOW_NONE
}
motionLayout.setDebugMode(debugMode)
binding.motionLayout.setDebugMode(debugMode)
}
}
Loading