티스토리 뷰
DataBinding + LiveData + BindingAdapter
build.gradle (Module: app)
android {
dataBinding {
enabled = true
}
}
BindingAdapters.java
package com.jwsoft.javaproject;
import android.view.View;
import androidx.annotation.ColorInt;
import androidx.databinding.BindingAdapter;
public class BindingAdapters {
@BindingAdapter("android:visibleJW")
public static void setVisible(View view, Boolean bool) {
if (bool) view.setVisibility(View.VISIBLE);
else view.setVisibility(View.INVISIBLE);
}
@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>
<data>
<variable
name="mainActivity"
type="com.jwsoft.javaproject.MainActivity" />
</data>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:colorJW="@{mainActivity.liveColor}"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="혀가 길지 않은 개발자\nJames Kim"
android:textSize="30dp"
android:textStyle="bold"
android:textAlignment="center"
android:visibleJW="@{mainActivity.liveVisible}"
/>
<Button
android:id="@+id/btnVisible"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:textAllCaps="false"
android:text="Visible"
android:textSize="40dp"
android:textStyle="italic|bold"
android:layout_marginTop="30dp" />
<Button
android:id="@+id/btnInvisible"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:textAllCaps="false"
android:text="Invisible"
android:textSize="40dp"
android:textStyle="italic|bold" />
<Button
android:id="@+id/btnChange"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:textAllCaps="false"
android:text="Change Background Color"
android:textSize="20dp"
android:textStyle="italic|bold"
android:layout_marginTop="100dp"/>
</LinearLayout>
</layout>
MainActivity.java
package com.jwsoft.javaproject;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.MutableLiveData;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import com.jwsoft.javaproject.databinding.ActivityMainBinding;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
private ActivityMainBinding binding;
public MutableLiveData<Boolean> liveVisible = new MutableLiveData<>();
public MutableLiveData<Integer> liveColor = new MutableLiveData<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
binding.setMainActivity(this);
binding.setLifecycleOwner(this);
binding.btnVisible.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
liveVisible.setValue(true);
}
});
binding.btnInvisible.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
liveVisible.setValue(false);
}
});
binding.btnChange.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int r = new Random().nextInt(256);
int g = new Random().nextInt(256);
int b = new Random().nextInt(256);
liveColor.setValue(Color.argb(255, r, g, b));
}
});
}
}
liveVisible 변수가 null 값이라 에러 발생.
코틀린은 초기화 안 해도 BindingAdapter에 자동으로 false 값 넘어감.
MainActivity.java
package com.jwsoft.javaproject;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.MutableLiveData;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import com.jwsoft.javaproject.databinding.ActivityMainBinding;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
private ActivityMainBinding binding;
public MutableLiveData<Boolean> liveVisible = new MutableLiveData<>();
public MutableLiveData<Integer> liveColor = new MutableLiveData<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
binding.setMainActivity(this);
binding.setLifecycleOwner(this);
liveVisible.setValue(false); // 초기화 안 해주면 에러 발생.
binding.btnVisible.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
liveVisible.setValue(true);
}
});
binding.btnInvisible.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
liveVisible.setValue(false);
}
});
binding.btnChange.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int r = new Random().nextInt(256);
int g = new Random().nextInt(256);
int b = new Random().nextInt(256);
liveColor.setValue(Color.argb(255, r, g, b));
}
});
}
}
색상 랜덤 추출 참고
stackoverflow.com/questions/5280367/android-generate-random-color-on-click
'Android > Java' 카테고리의 다른 글
[Java] SQLite (0) | 2020.07.11 |
---|---|
[Java] ViewModel (0) | 2020.07.10 |
[Java] DataBinding + LiveData (0) | 2020.07.09 |
[Java] LiveData (0) | 2020.07.09 |
[Java] DataBinding (0) | 2020.07.08 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- XML
- ViewModel
- 코틀린
- JSONObject
- Intent
- Livedata
- Architecture Pattern
- 혀가 길지 않은 개발자
- activity
- coroutine
- Kotlin
- 자바
- 안드로이드 #코틀린 #Android #Kotlin
- 안드로이드
- Android
- Design Pattern
- JSONArray
- DataBinding
- ArrayList
- ViewPager2
- fragment
- TabLayout
- Vue.js #Vue.js + javascript
- James Kim
- CoordinatorLayout
- handler
- View
- recyclerview
- java
- MVVM
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함