Skip to content

Commit

Permalink
[Feat] Step02 데이터 바인딩 예제 실습
Browse files Browse the repository at this point in the history
DataBindingAdpater 생성. UserData를 이용해 데이터 바인딩. 

Related to #3
  • Loading branch information
LEE-HYUNGJUN committed Dec 31, 2020
1 parent 32f7852 commit 71df34a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,13 @@ import com.bumptech.glide.Glide
* 뷰에서 사용할 어트리뷰트 이름은 image로 합니다.*/
object BindingAdapter {


@JvmStatic
@BindingAdapter("showImage")
fun showImage(view: ImageView, imageUrl: String){
Glide.with(view.context)
.load(imageUrl)
.into(view)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,26 @@ package sotp.semina.practicegitanddatabinding
import android.os.Bundle
import android.widget.EditText
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.BindingAdapter
import androidx.databinding.DataBindingUtil
import sotp.semina.practicegitanddatabinding.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {

private lateinit var bindingAdapter: BindingAdapter

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val binding: ActivityMainBinding =
DataBindingUtil.setContentView(this, R.layout.activity_main)
/*TODO step3 유저 데이터를 binding 변수에 넣어서 뷰에서 사용할 수 있도록 합니다.*/

binding.userProfile = UserData(IMAGE_URL, DEFAULT_USER_NAME)
addButtonClickEvent(binding)
}

private fun setProfileName(binding: ActivityMainBinding, content: String) {
binding.userProfile = UserData(IMAGE_URL,content)

}

private fun addButtonClickEvent(binding: ActivityMainBinding) {
Expand Down
8 changes: 5 additions & 3 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@
xmlns:tools="http://schemas.android.com/tools">

<data>
<!--TODO step1 데이터 클래스 결합-->
<variable
name="userProfile"
type="sotp.semina.practicegitanddatabinding.UserData" />
</data>

<!--TODO step3 아래를 유저 데이터로 결합해주세요-->
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<ImageView
android:id="@+id/image_profile_image"
showImage="@{userProfile.userImage}"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_marginTop="100dp"
Expand All @@ -33,7 +35,7 @@
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/image_profile_image"
tools:text="profile name" />
tools:text="@{userProfile.userName}" />

<EditText
android:id="@+id/input_changed_profile"
Expand Down

0 comments on commit 71df34a

Please sign in to comment.