개인 공부

MVVM 패턴 ( 데이터 바인딩 )예제 1

Machine_웅 2019. 9. 23. 18:34
728x90
반응형

<activity_main.xml>

 

<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <data>
        <!-- User 클래스에 바인딩한다 -->
        <variable
            name="user"
            type="com.example.jeon.mvvm_test.User"/>
    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        >

        <!-- 시간을 표시한다 -->
        <TextView
            android:id="@+id/text_time"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"/>

        <!-- 프로필 -->
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="프로필"
            android:textAppearance="@style/TextAppearance.AppCompat.Large"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="이름:"
            android:textAppearance="@style/TextAppearance.AppCompat.Medium"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{user.name}"
            android:textAppearance="@style/TextAppearance.AppCompat.Medium"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="나이:"
            android:textAppearance="@style/TextAppearance.AppCompat.Medium"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:text="@{String.valueOf(user.age)}"
            android:textAppearance="@style/TextAppearance.AppCompat.Medium"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:text="LIKE:"
            android:textAppearance="@style/TextAppearance.AppCompat.Medium"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:text="@{String.valueOf(user.like)}"
            android:textAppearance="@style/TextAppearance.AppCompat.Medium"/>


        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="@{user::onClickLike}"
            android:src="@drawable/ic_launcher_background"
            />

    </LinearLayout>


</layout>



 

<User 클래스>

public class User {

    public ObservableField<String> name = new ObservableField<>();
    public ObservableInt age = new ObservableInt();
    public ObservableInt like = new ObservableInt();


    public User(String nameString, int ageInt){
        name.set(nameString);
        age.set(ageInt);
        like.set(0);
    }

    public void onClickLike(View view){
        like.set(like.get()+1);
    }

}

<메인 엑티비티 클래스>

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //  << 클레스와 레이아웃의 연결 >>

        // 바인딩 객체를 얻는다.
        ActivityMainBinding binding = DataBindingUtil.setContentView(this,R.layout.activity_main);

        // 바인딩 객체에 User 를 설정한다.
        binding.setUser(new User("kim",25));

        String date = (String) DateFormat.format("yyyy/MM/dd kk:mm:ss",Calendar.getInstance());
        binding.textTime.setText(date);

    }
}
728x90
반응형