Skip to content

Commit 21a726c

Browse files
Merge pull request #7 from yogeshpaliyal/feature/binding_adapter
Feature/binding adapter
2 parents e537e87 + ff94fc4 commit 21a726c

File tree

11 files changed

+175
-7
lines changed

11 files changed

+175
-7
lines changed

.idea/compiler.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 8 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/src/main/AndroidManifest.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111
android:roundIcon="@mipmap/ic_launcher_round"
1212
android:supportsRtl="true"
1313
android:theme="@style/AppTheme">
14-
<activity android:name=".ui.activity.LoadingListingActivity"></activity>
14+
<activity
15+
android:name=".ui.activity.BindingAdapterTestActivity"
16+
android:exported="true" />
17+
<activity android:name=".ui.activity.LoadingListingActivity" />
1518
<activity android:name=".ui.activity.MainActivity">
1619
<intent-filter>
1720
<action android:name="android.intent.action.MAIN" />

app/src/main/java/com/techpaliyal/androidkotlinmvvm/extensions/ImageViewExtensions.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import com.bumptech.glide.Glide
1313
* Created Date : 9 January 2020
1414
*/
1515
@BindingAdapter("tool:loadUrl")
16-
fun loadUrl(imageView: ImageView?, loadUrl: String) {
16+
fun loadUrl(imageView: ImageView?, loadUrl: String?) {
1717
imageView?.let {
1818

1919
Glide.with(imageView).load(loadUrl).into(imageView)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.techpaliyal.androidkotlinmvvm.ui.activity
2+
3+
import androidx.appcompat.app.AppCompatActivity
4+
import android.os.Bundle
5+
import com.techpaliyal.androidkotlinmvvm.R
6+
import com.techpaliyal.androidkotlinmvvm.databinding.ActivityBindingAdapterTestBinding
7+
import com.techpaliyal.androidkotlinmvvm.ui.view_model.BindingTestViewModel
8+
import com.techpaliyal.androidkotlinmvvm.ui.view_model.initViewModel
9+
10+
class BindingAdapterTestActivity : AppCompatActivity() {
11+
12+
private lateinit var binding: ActivityBindingAdapterTestBinding
13+
14+
private val mViewModel by lazy {
15+
initViewModel(BindingTestViewModel::class.java)
16+
}
17+
18+
override fun onCreate(savedInstanceState: Bundle?) {
19+
super.onCreate(savedInstanceState)
20+
binding = ActivityBindingAdapterTestBinding.inflate(layoutInflater)
21+
setContentView(binding.root)
22+
23+
binding.lifecycleOwner = this
24+
binding.myLifecycleOwner = this
25+
binding.mViewModel = mViewModel
26+
binding.executePendingBindings()
27+
28+
}
29+
}

app/src/main/java/com/techpaliyal/androidkotlinmvvm/ui/activity/MainActivity.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.techpaliyal.androidkotlinmvvm.ui.activity
22

3+
import android.content.Intent
34
import android.os.Bundle
45
import androidx.appcompat.app.AppCompatActivity
56
import androidx.databinding.DataBindingUtil
@@ -42,8 +43,9 @@ class MainActivity : AppCompatActivity() {
4243
ShimmerListingActivity.start(this)
4344
}
4445

45-
binding.btnPaginationListing.setOnClickListener {
46-
PaginationListingActivity.start(this)
46+
binding.btnBindingAdapter.setOnClickListener {
47+
val intent = Intent(this, BindingAdapterTestActivity::class.java)
48+
startActivity(intent)
4749
}
4850

4951
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.techpaliyal.androidkotlinmvvm.ui.view_model
2+
3+
import android.app.Application
4+
import androidx.lifecycle.*
5+
import com.techpaliyal.androidkotlinmvvm.model.UserModel
6+
import com.yogeshpaliyal.universal_adapter.utils.Resource
7+
import kotlinx.coroutines.delay
8+
import kotlinx.coroutines.launch
9+
10+
class BindingTestViewModel() : ViewModel() {
11+
12+
private val _usersData = MutableLiveData<Resource<List<UserModel>>>()
13+
val usersData : LiveData<Resource<List<UserModel>>> = _usersData
14+
15+
16+
init {
17+
loadData()
18+
}
19+
20+
private fun loadData(){
21+
22+
viewModelScope.launch {
23+
_usersData.postValue(Resource.loading())
24+
delay(3000)
25+
val tempArr = ArrayList<UserModel>()
26+
tempArr.add(UserModel(name = "Yogesh",image = "https://randomuser.me/api/portraits/men/52.jpg", address = "Jodhpur"))
27+
tempArr.add(UserModel(name = "Umesh",image = "https://randomuser.me/api/portraits/men/62.jpg"))
28+
tempArr.add(UserModel(name = "Sohan",image = "https://randomuser.me/api/portraits/men/84.jpg"))
29+
tempArr.add(UserModel(name = "Jitendra",image = "https://randomuser.me/api/portraits/men/83.jpg"))
30+
_usersData.postValue(Resource.success(tempArr))
31+
}
32+
33+
}
34+
35+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<layout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
4+
xmlns:tools="http://schemas.android.com/tools">
5+
6+
<data>
7+
<import type="com.techpaliyal.androidkotlinmvvm.R" />
8+
9+
<variable
10+
name="myLifecycleOwner"
11+
type="androidx.lifecycle.LifecycleOwner" />
12+
13+
<variable
14+
name="mViewModel"
15+
type="com.techpaliyal.androidkotlinmvvm.ui.view_model.BindingTestViewModel" />
16+
</data>
17+
18+
<androidx.constraintlayout.widget.ConstraintLayout
19+
android:layout_width="match_parent"
20+
android:layout_height="match_parent"
21+
tools:context=".ui.activity.BindingAdapterTestActivity">
22+
23+
<androidx.recyclerview.widget.RecyclerView
24+
android:layout_width="0dp"
25+
android:layout_height="0dp"
26+
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
27+
android:orientation="vertical"
28+
item_layout="@{R.layout.item_user}"
29+
loading_layout="@{R.layout.item_user_shimmer}"
30+
data="@{mViewModel.usersData}"
31+
lifecycleOwner="@{myLifecycleOwner}"
32+
app:layout_constraintTop_toTopOf="parent"
33+
app:layout_constraintStart_toStartOf="parent"
34+
app:layout_constraintBottom_toBottomOf="parent"
35+
app:layout_constraintEnd_toEndOf="parent"/>
36+
37+
</androidx.constraintlayout.widget.ConstraintLayout>
38+
</layout>

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,17 @@
7272
android:text="Pagination Listing"
7373
app:layout_constraintBottom_toBottomOf="parent"/>
7474

75+
76+
<Button
77+
android:id="@+id/btnBindingAdapter"
78+
app:layout_constraintTop_toBottomOf="@id/btnUserListing"
79+
app:layout_constraintStart_toStartOf="parent"
80+
app:layout_constraintEnd_toEndOf="parent"
81+
android:layout_width="match_parent"
82+
android:layout_height="wrap_content"
83+
android:text="Binding Adapter Test"
84+
app:layout_constraintBottom_toBottomOf="parent"/>
85+
7586
</LinearLayout>
7687
</ScrollView>
7788
</layout>

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ buildscript {
99
}
1010
dependencies {
1111
classpath 'com.android.tools.build:gradle:4.2.2'
12-
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
12+
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.21"
1313
// NOTE: Do not place your application dependencies here; they belong
1414
// in the individual module build.gradle files
1515
}

0 commit comments

Comments
 (0)