티스토리 뷰
ViewModel + DataBinding + LiveData + BindingAdapter
build.gradle (Module: app)
android {
dataBinding {
enabled = true
}
}
MainViewModel.java
package com.jwsoft.javaproject;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
public class MainViewModel extends ViewModel {
public MutableLiveData<Integer> liveColor = new MutableLiveData<>();
}
BindingAdapters.java
package com.jwsoft.javaproject;
import android.view.View;
import androidx.annotation.ColorInt;
import androidx.databinding.BindingAdapter;
public class BindingAdapters {
@BindingAdapter("android:colorJW")
public static void setColor(View view, @ColorInt int color) {
view.setBackgroundColor(color);
}
}
코틀린은 build.gradle (Module: app) 안에
apply plugin: 'kotlin-kapt' 추가해야
@BindingAdapter 어노테이션 사용 가능
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="mainActivity"
type="com.jwsoft.javaproject.MainActivity" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:colorJW="@{mainActivity.mainViewModel.liveColor}"
tools:context=".MainActivity">
<Button
android:id="@+id/btnRed"
android:layout_width="200dp"
android:layout_height="100dp"
android:textAllCaps="false"
android:text="Red"
android:textSize="30dp"
android:textStyle="bold|italic" />
<Button
android:id="@+id/btnGreen"
android:layout_width="200dp"
android:layout_height="100dp"
android:textAllCaps="false"
android:text="Green"
android:textSize="30dp"
android:textStyle="bold|italic" />
<Button
android:id="@+id/btnBlue"
android:layout_width="200dp"
android:layout_height="100dp"
android:textAllCaps="false"
android:text="Blue"
android:textSize="30dp"
android:textStyle="bold|italic" />
</LinearLayout>
</layout>
MainActivity.java
package com.jwsoft.javaproject;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import com.jwsoft.javaproject.databinding.ActivityMainBinding;
public class MainActivity extends AppCompatActivity {
private ActivityMainBinding binding;
public MainViewModel mainViewModel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
binding.setMainActivity(this);
binding.setLifecycleOwner(this);
mainViewModel = new ViewModelProvider(this, new ViewModelProvider.NewInstanceFactory()).get(MainViewModel.class);
binding.btnRed.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mainViewModel.liveColor.setValue(Color.rgb(255, 121, 121));
}
});
binding.btnGreen.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mainViewModel.liveColor.setValue(Color.rgb(121, 255, 121));
}
});
binding.btnBlue.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mainViewModel.liveColor.setValue(Color.rgb(121, 121, 255));
}
});
}
}
'Android > Java' 카테고리의 다른 글
[Java] CardView (0) | 2020.07.11 |
---|---|
[Java] SQLite (0) | 2020.07.11 |
[Java] DataBinding + LiveData + BindingAdapter (0) | 2020.07.09 |
[Java] DataBinding + LiveData (0) | 2020.07.09 |
[Java] LiveData (0) | 2020.07.09 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- DataBinding
- handler
- fragment
- MVVM
- Android
- ViewModel
- Design Pattern
- 혀가 길지 않은 개발자
- Livedata
- 코틀린
- TabLayout
- java
- JSONObject
- CoordinatorLayout
- XML
- ViewPager2
- View
- Architecture Pattern
- ArrayList
- JSONArray
- 안드로이드 #코틀린 #Android #Kotlin
- Vue.js #Vue.js + javascript
- coroutine
- James Kim
- 안드로이드
- 자바
- Intent
- Kotlin
- activity
- recyclerview
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
글 보관함