Skip to content

Commit af665b7

Browse files
Merge pull request #39 from DylanCaiCoding/2.0.0
2.0.0
2 parents df77745 + 5e1c356 commit af665b7

Some content is hidden

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

41 files changed

+1022
-469
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.dylanc.viewbinding.sample
2+
3+
import android.os.Bundle
4+
import android.os.Handler
5+
import android.os.Looper
6+
import android.view.View
7+
import androidx.fragment.app.Fragment
8+
import com.dylanc.viewbinding.binding
9+
import com.dylanc.viewbinding.sample.databinding.FragmentCustomViewBinding
10+
import com.dylanc.viewbinding.sample.widget.LoadingDialogFragment
11+
12+
class CustomViewFragment : Fragment(R.layout.fragment_custom_view) {
13+
14+
private val binding: FragmentCustomViewBinding by binding()
15+
private val loadingDialog by lazy { LoadingDialogFragment() }
16+
private val handler = Handler(Looper.getMainLooper())
17+
18+
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
19+
super.onViewCreated(view, savedInstanceState)
20+
binding.customView.setOnClickListener {
21+
loadingDialog.show(childFragmentManager, "loading")
22+
handler.postDelayed({
23+
loadingDialog.dismiss()
24+
}, 2000)
25+
}
26+
}
27+
}

app/src/main/java/com/dylanc/viewbinding/sample/HomeFragment.kt

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,37 @@ package com.dylanc.viewbinding.sample
44

55
import android.os.Bundle
66
import android.view.View
7+
import android.widget.Toast
78
import androidx.fragment.app.Fragment
8-
import androidx.viewbinding.ViewBinding
9-
import com.dylanc.viewbinding.BindingLifecycleOwner
10-
import com.dylanc.viewbinding.binding
9+
import com.dylanc.viewbinding.base.simpleListAdapter
10+
import com.dylanc.viewbinding.nonreflection.binding
1111
import com.dylanc.viewbinding.sample.databinding.FragmentHomeBinding
12+
import com.dylanc.viewbinding.sample.databinding.ItemFooBinding
13+
import com.dylanc.viewbinding.sample.item.Foo
14+
import com.dylanc.viewbinding.sample.item.FooDiffCallback
1215

1316
/**
1417
* @author Dylan Cai
1518
*/
16-
class HomeFragment : Fragment(R.layout.fragment_home), BindingLifecycleOwner {
19+
class HomeFragment : Fragment(R.layout.fragment_home) {
1720

18-
private val binding: FragmentHomeBinding by binding()
21+
private val binding by binding(FragmentHomeBinding::bind)
22+
23+
private val list = listOf(Foo("item 1"), Foo("item 2"), Foo("item 3"))
1924

2025
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
2126
super.onViewCreated(view, savedInstanceState)
22-
binding.tvHelloWorld.text = "Hello Android!"
27+
binding.recyclerView.adapter = adapter
28+
adapter.submitList(list)
29+
adapter.doOnItemClick { item, _ ->
30+
Toast.makeText(requireContext(), item.value, Toast.LENGTH_SHORT).show()
31+
}
32+
adapter.doOnItemLongClick { item, _ ->
33+
Toast.makeText(requireContext(), "long click ${item.value}", Toast.LENGTH_SHORT).show()
34+
}
2335
}
2436

25-
override fun onDestroyViewBinding(destroyingBinding: ViewBinding) {
26-
// Release something before destroying the binding
37+
private val adapter = simpleListAdapter<Foo, ItemFooBinding>(FooDiffCallback()) { item ->
38+
tvFoo.text = item.value
2739
}
28-
2940
}

app/src/main/java/com/dylanc/viewbinding/sample/MainActivity.kt

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,44 +17,45 @@
1717
package com.dylanc.viewbinding.sample
1818

1919
import android.os.Bundle
20-
import android.os.Handler
21-
import android.os.Looper
2220
import androidx.appcompat.app.AppCompatActivity
23-
import androidx.recyclerview.widget.DiffUtil
24-
import com.dylanc.viewbinding.ListAdapter
21+
import androidx.viewpager2.adapter.FragmentStateAdapter
22+
import com.dylanc.viewbinding.doOnCustomTabSelected
2523
import com.dylanc.viewbinding.nonreflection.binding
24+
import com.dylanc.viewbinding.nonreflection.setCustomView
2625
import com.dylanc.viewbinding.sample.databinding.ActivityMainBinding
27-
import com.dylanc.viewbinding.sample.databinding.ItemFooBinding
28-
import com.dylanc.viewbinding.sample.item.Foo
29-
import com.dylanc.viewbinding.sample.widget.LoadingDialogFragment
26+
import com.dylanc.viewbinding.sample.databinding.LayoutBottomTabBinding
27+
import com.google.android.material.tabs.TabLayoutMediator
3028

3129
class MainActivity : AppCompatActivity() {
3230

3331
private val binding by binding(ActivityMainBinding::inflate)
34-
private val loadingDialog by lazy { LoadingDialogFragment() }
35-
private val handler = Handler(Looper.getMainLooper())
36-
private val list = listOf(Foo("item 1"), Foo("item 2"), Foo("item 3"))
3732

3833
override fun onCreate(savedInstanceState: Bundle?) {
3934
super.onCreate(savedInstanceState)
40-
with(binding) {
41-
customView.setOnClickListener {
42-
loadingDialog.show(supportFragmentManager, "loading")
43-
handler.postDelayed({
44-
loadingDialog.dismiss()
45-
}, 2000)
46-
}
47-
recyclerView.adapter = adapter
48-
adapter.submitList(list)
35+
36+
val fragments = listOf(HomeFragment(), CustomViewFragment())
37+
binding.viewPager2.adapter = object : FragmentStateAdapter(this) {
38+
override fun getItemCount() = fragments.size
39+
override fun createFragment(position: Int) = fragments[position]
4940
}
50-
}
5141

52-
private val adapter = ListAdapter<Foo, ItemFooBinding>(DiffCallback()) {
53-
binding.tvFoo.text = it.value
54-
}
42+
TabLayoutMediator(binding.tabLayout, binding.viewPager2, false) { tab, position ->
43+
tab.setCustomView(LayoutBottomTabBinding::inflate) {
44+
if (position == 0) {
45+
textView.textSize = 16f
46+
}
47+
}
48+
}.attach()
5549

56-
class DiffCallback : DiffUtil.ItemCallback<Foo>() {
57-
override fun areItemsTheSame(oldItem: Foo, newItem: Foo) = oldItem.value == newItem.value
58-
override fun areContentsTheSame(oldItem: Foo, newItem: Foo) = oldItem.value == newItem.value
50+
binding.tabLayout.doOnCustomTabSelected<LayoutBottomTabBinding>(
51+
// binding.tabLayout.doOnCustomTabSelected(LayoutBottomTabBinding::bind,
52+
onTabSelected = {
53+
textView.textSize = 16f
54+
},
55+
onTabUnselected = {
56+
textView.textSize = 14f
57+
}
58+
)
5959
}
60+
6061
}

app/src/main/java/com/dylanc/viewbinding/sample/base/nonreflection/kotlin/BaseBindingFragment.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
@file:Suppress("unused", "MemberVisibilityCanBePrivate")
17+
@file:Suppress("unused")
1818

1919
package com.dylanc.viewbinding.sample.base.nonreflection.kotlin
2020

app/src/main/java/com/dylanc/viewbinding/sample/base/nonreflection/kotlin/BaseBindingQuickAdapter.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ import android.view.ViewGroup
2424
import androidx.viewbinding.ViewBinding
2525
import com.chad.library.adapter.base.BaseQuickAdapter
2626
import com.chad.library.adapter.base.viewholder.BaseViewHolder
27-
import com.dylanc.viewbinding.BindingViewHolder
28-
import com.dylanc.viewbinding.base.inflateBindingWithGeneric
2927

3028

3129
abstract class BaseBindingQuickAdapter<T, VB : ViewBinding>(

app/src/main/java/com/dylanc/viewbinding/sample/base/reflection/kotlin/BaseBindingActivity.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ package com.dylanc.viewbinding.sample.base.reflection.kotlin
2121
import android.os.Bundle
2222
import androidx.appcompat.app.AppCompatActivity
2323
import androidx.viewbinding.ViewBinding
24-
import com.dylanc.viewbinding.base.inflateBindingWithGeneric
24+
import com.dylanc.viewbinding.base.ViewBindingUtil
2525

2626
/**
2727
* How to modify the base class to use view binding, you need the following steps:
@@ -40,7 +40,7 @@ abstract class BaseBindingActivity<VB : ViewBinding> : AppCompatActivity() {
4040

4141
override fun onCreate(savedInstanceState: Bundle?) {
4242
super.onCreate(savedInstanceState)
43-
binding = inflateBindingWithGeneric(layoutInflater)
43+
binding = ViewBindingUtil.inflateWithGeneric(this, layoutInflater)
4444
setContentView(binding.root)
4545
}
4646
}

app/src/main/java/com/dylanc/viewbinding/sample/base/reflection/kotlin/BaseBindingDialog.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import android.app.Dialog
2222
import android.content.Context
2323
import android.os.Bundle
2424
import androidx.viewbinding.ViewBinding
25-
import com.dylanc.viewbinding.base.inflateBindingWithGeneric
25+
import com.dylanc.viewbinding.base.ViewBindingUtil
2626

2727
/**
2828
* How to modify the base class to use view binding, you need the following steps:
@@ -42,7 +42,7 @@ abstract class BaseBindingDialog<VB : ViewBinding>(context: Context, themeResId:
4242

4343
override fun onCreate(savedInstanceState: Bundle?) {
4444
super.onCreate(savedInstanceState)
45-
binding = inflateBindingWithGeneric(layoutInflater)
45+
binding = ViewBindingUtil.inflateWithGeneric(this, layoutInflater)
4646
setContentView(binding.root)
4747
}
4848
}

app/src/main/java/com/dylanc/viewbinding/sample/base/reflection/kotlin/BaseBindingFragment.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import android.view.View
2424
import android.view.ViewGroup
2525
import androidx.fragment.app.Fragment
2626
import androidx.viewbinding.ViewBinding
27-
import com.dylanc.viewbinding.base.inflateBindingWithGeneric
27+
import com.dylanc.viewbinding.base.ViewBindingUtil
2828

2929
/**
3030
* How to modify the base class to use view binding, you need the following steps:
@@ -43,7 +43,7 @@ abstract class BaseBindingFragment<VB : ViewBinding> : Fragment() {
4343
val binding: VB get() = _binding!!
4444

4545
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
46-
_binding = inflateBindingWithGeneric(layoutInflater, container, false)
46+
_binding = ViewBindingUtil.inflateWithGeneric(this, layoutInflater, container, false)
4747
return binding.root
4848
}
4949

app/src/main/java/com/dylanc/viewbinding/sample/base/reflection/kotlin/BaseBindingQuickAdapter.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import android.view.ViewGroup
2323
import androidx.viewbinding.ViewBinding
2424
import com.chad.library.adapter.base.BaseQuickAdapter
2525
import com.chad.library.adapter.base.viewholder.BaseViewHolder
26-
import com.dylanc.viewbinding.base.inflateBindingWithGeneric
26+
import com.dylanc.viewbinding.base.ViewBindingUtil
2727

2828
/**
2929
* How to modify the base adapter class to use view binding, you need the following steps:
@@ -40,7 +40,7 @@ abstract class BaseBindingQuickAdapter<T, VB : ViewBinding>(layoutResId: Int = -
4040
BaseQuickAdapter<T, BaseBindingQuickAdapter.BaseBindingHolder>(layoutResId) {
4141

4242
override fun onCreateDefViewHolder(parent: ViewGroup, viewType: Int) =
43-
BaseBindingHolder(inflateBindingWithGeneric<VB>(parent))
43+
BaseBindingHolder(ViewBindingUtil.inflateWithGeneric<VB>(this, parent))
4444

4545
class BaseBindingHolder(private val binding: ViewBinding) : BaseViewHolder(binding.root) {
4646
constructor(itemView: View) : this(ViewBinding { itemView })

app/src/main/java/com/dylanc/viewbinding/sample/base/reflection/kotlin/BindingViewDelegate.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,16 @@ import android.view.ViewGroup
2121
import androidx.recyclerview.widget.RecyclerView
2222
import androidx.viewbinding.ViewBinding
2323
import com.drakeet.multitype.ItemViewDelegate
24-
import com.dylanc.viewbinding.base.inflateBindingWithGeneric
24+
import com.dylanc.viewbinding.base.ViewBindingUtil
2525

2626
/**
2727
* @author Dylan Cai
2828
*/
2929
abstract class BindingViewDelegate<T, VB : ViewBinding> :
3030
ItemViewDelegate<T, BindingViewDelegate.BindingViewHolder<VB>>() {
3131

32-
override fun onCreateViewHolder(context: Context, parent: ViewGroup): BindingViewHolder<VB> {
33-
return BindingViewHolder(inflateBindingWithGeneric(parent))
34-
}
32+
override fun onCreateViewHolder(context: Context, parent: ViewGroup) =
33+
BindingViewHolder(ViewBindingUtil.inflateWithGeneric<VB>(this, parent))
3534

3635
class BindingViewHolder<VB : ViewBinding>(val binding: VB) : RecyclerView.ViewHolder(binding.root)
3736
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.dylanc.viewbinding.sample.item
2+
3+
import androidx.recyclerview.widget.DiffUtil
4+
5+
class FooDiffCallback : DiffUtil.ItemCallback<Foo>() {
6+
override fun areItemsTheSame(oldItem: Foo, newItem: Foo) = oldItem.value == newItem.value
7+
override fun areContentsTheSame(oldItem: Foo, newItem: Foo) = oldItem.value == newItem.value
8+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.dylanc.viewbinding.sample.item
2+
3+
import android.view.LayoutInflater
4+
import android.view.ViewGroup
5+
import androidx.recyclerview.widget.DiffUtil
6+
import androidx.recyclerview.widget.ListAdapter
7+
import androidx.viewbinding.ViewBinding
8+
import com.dylanc.viewbinding.BindingViewHolder
9+
10+
inline fun <T, reified VB : ViewBinding> listAdapter(
11+
diffCallback: DiffUtil.ItemCallback<T>,
12+
crossinline onBindViewHolder: BindingViewHolder<VB>.(T) -> Unit
13+
) = object : ListAdapter<T, BindingViewHolder<VB>>(diffCallback) {
14+
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) =
15+
BindingViewHolder<VB>(parent)
16+
17+
override fun onBindViewHolder(holder: BindingViewHolder<VB>, position: Int) {
18+
onBindViewHolder(holder, currentList[position])
19+
}
20+
}
21+
22+
inline fun <T, reified VB : ViewBinding> listAdapter(
23+
diffCallback: DiffUtil.ItemCallback<T>,
24+
noinline inflate: (LayoutInflater, ViewGroup, Boolean) -> VB,
25+
crossinline onBindViewHolder: com.dylanc.viewbinding.nonreflection.BindingViewHolder<VB>.(T) -> Unit
26+
) = object : ListAdapter<T, com.dylanc.viewbinding.nonreflection.BindingViewHolder<VB>>(diffCallback) {
27+
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) =
28+
com.dylanc.viewbinding.nonreflection.BindingViewHolder(parent, inflate)
29+
30+
override fun onBindViewHolder(holder: com.dylanc.viewbinding.nonreflection.BindingViewHolder<VB>, position: Int) {
31+
onBindViewHolder(holder, currentList[position])
32+
}
33+
}

app/src/main/res/layout/activity_main.xml

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,17 @@
66
android:layout_height="match_parent"
77
tools:context=".MainActivity">
88

9-
<androidx.recyclerview.widget.RecyclerView
10-
android:id="@+id/recycler_view"
9+
<androidx.viewpager2.widget.ViewPager2
10+
android:id="@+id/view_pager_2"
1111
android:layout_width="match_parent"
12-
android:layout_height="wrap_content"
13-
app:layout_constraintEnd_toEndOf="parent"
14-
app:layout_constraintStart_toStartOf="parent"
15-
app:layout_constraintTop_toTopOf="parent"
16-
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"/>
12+
android:layout_height="0dp"
13+
app:layout_constraintBottom_toTopOf="@+id/tab_layout"
14+
app:layout_constraintTop_toTopOf="parent" />
1715

18-
<com.dylanc.viewbinding.sample.widget.CustomView
19-
android:id="@+id/custom_view"
20-
android:layout_width="wrap_content"
21-
android:layout_height="wrap_content"
22-
android:layout_marginBottom="16dp"
23-
app:layout_constraintBottom_toBottomOf="parent"
24-
app:layout_constraintEnd_toEndOf="parent"
25-
app:layout_constraintStart_toStartOf="parent" />
16+
<com.google.android.material.tabs.TabLayout
17+
android:id="@+id/tab_layout"
18+
android:layout_width="match_parent"
19+
android:layout_height="48dp"
20+
app:layout_constraintBottom_toBottomOf="parent" />
2621

2722
</androidx.constraintlayout.widget.ConstraintLayout>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
4+
android:layout_width="match_parent"
5+
android:layout_height="match_parent">
6+
7+
<com.dylanc.viewbinding.sample.widget.CustomView
8+
android:id="@+id/custom_view"
9+
android:layout_width="wrap_content"
10+
android:layout_height="wrap_content"
11+
app:layout_constraintBottom_toBottomOf="parent"
12+
app:layout_constraintEnd_toEndOf="parent"
13+
app:layout_constraintStart_toStartOf="parent"
14+
app:layout_constraintTop_toTopOf="parent" />
15+
16+
</androidx.constraintlayout.widget.ConstraintLayout>

app/src/main/res/layout/fragment_home.xml

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@
33
xmlns:app="http://schemas.android.com/apk/res-auto"
44
android:layout_width="match_parent" android:layout_height="match_parent">
55

6-
<TextView
7-
android:id="@+id/tv_hello_world"
8-
android:layout_width="wrap_content"
6+
<androidx.recyclerview.widget.RecyclerView
7+
android:id="@+id/recycler_view"
8+
android:layout_width="match_parent"
99
android:layout_height="wrap_content"
10-
android:text="TextView"
11-
app:layout_constraintBottom_toBottomOf="parent"
1210
app:layout_constraintEnd_toEndOf="parent"
1311
app:layout_constraintStart_toStartOf="parent"
14-
app:layout_constraintTop_toTopOf="parent" />
12+
app:layout_constraintTop_toTopOf="parent"
13+
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"/>
1514
</androidx.constraintlayout.widget.ConstraintLayout>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
4+
android:layout_width="match_parent"
5+
android:layout_height="match_parent">
6+
7+
<TextView
8+
android:id="@+id/textView"
9+
android:layout_width="wrap_content"
10+
android:layout_height="wrap_content"
11+
android:text="TextView"
12+
app:layout_constraintBottom_toBottomOf="parent"
13+
app:layout_constraintEnd_toEndOf="parent"
14+
app:layout_constraintStart_toStartOf="parent"
15+
app:layout_constraintTop_toTopOf="parent" />
16+
</androidx.constraintlayout.widget.ConstraintLayout>

0 commit comments

Comments
 (0)