티스토리 뷰

 

 

#1 버전 1 

1. ViewModel을 상속받을 수 있도록 다른 class 생성하기

- 생성자? 값을 초기화할 수 있도록 도와주는것

- MutableLiveData -> livedata 박스

- counter.setValue()  & counter.getValue() 

 

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
package com.example.myapplication2;
import android.view.View;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
 
public class CounterViewModel extends ViewModel {
 
    //public int counter = 0;
 
    CounterViewModel(){
        counter.setValue(0);
    }
 
    public MutableLiveData<Integer> counter = new MutableLiveData<>() ;
 
    public void increase(){
        counter.setValue(counter.getValue() + 1);
    }
 
    public void decrease(){
        counter.setValue(counter.getValue() - 1);
    }
 
}
 
cs

 

2. 기본 Activity

- 기존에 lifecycle에 대한 ViewModel Provider에 대해서 gradle 의존성 추가 필요

- DataBindingUtil에 대해서 setContentView 사용

- ViewModelProvider을 사용해서 컨텐트 뷰 사용해보기 

 

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
32
33
34
35
36
37
38
39
40
41
package com.example.myapplication2;
 
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.lifecycle.ViewModelProviders;
 
import android.os.Bundle;
import android.renderscript.ScriptGroup;
 
import com.example.myapplication2.databinding.ActivityMainBinding;
 
public class MainActivity extends AppCompatActivity {
 
 
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
 
 
        CounterViewModel counterViewModel = ViewModelProviders.of(this).get(CounterViewModel.class);
 
 
        binding.testView.setText(counterViewModel.counter.getValue() + "");
        binding.fabAdd.setOnClickListener(v -> {
            counterViewModel.increase();
            binding.testView.setText(counterViewModel.counter.getValue()+ "");
        });
 
        binding.fabAdd.setOnClickListener(v -> {
            counterViewModel.decrease();
            binding.testView.setText(counterViewModel.counter.getValue()+ "");
        });
 
    }
}
cs

 

 

3. xml에서 binding 사용시 주의사항

- layout으로 한번 걸쳐주기

- gradle에 의존성 추가하기 

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
32
33
34
35
36
37
38
39
40
41
42
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">
<androidx.constraintlayout.widget.ConstraintLayout
 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
 
    <TextView
        android:id="@+id/testView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:textSize="80dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
 
    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab_add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="true"
        app:srcCompat="@drawable/ic_baseline_add_24"
        tools:layout_editor_absoluteX="395dp"
        tools:layout_editor_absoluteY="690dp" />
 
    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab_remove"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="true"
        app:srcCompat="@drawable/ic_baseline_remove_24"
        tools:layout_editor_absoluteX="72dp"
        tools:layout_editor_absoluteY="697dp" />
 
</androidx.constraintlayout.widget.ConstraintLayout>
 
</layout>
cs

 

 

 

추가)

 

 

mainBinding.setLifecycleOwner(this);
mainBinding.setViewModel(numberViewClass);을 사용 

 

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class MainActivity extends AppCompatActivity {
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        ActivityMainBinding mainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
 
        NumberViewClass numberViewClass = ViewModelProviders.of(this).get(NumberViewClass.class);
        mainBinding.setLifecycleOwner(this);
        mainBinding.setViewModel(numberViewClass);
        mainBinding.fabAdd.setOnClickListener(v -> { numberViewClass.increase(); });
        mainBinding.fabAdd.setOnClickListener(v -> { numberViewClass.decrease(); });
 
    }
}
cs

 

 

<출처>
1. 오준석의 생활코딩 - Youtube --> 강의보고 정리한 내용 

모던 안드로이드 - www.youtube.com/watch?v=pG6OkJ3rSjg&list=PLxTmPHxRH3VXHOBnaGQcbSGslbAjr8obc

 

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG more
«   2025/01   »
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
글 보관함