[안드로이드]RecyclerView_dataBinding
~아이템이 되는 레이아웃~
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="20dp">
<ImageView
android:id="@+id/iv_github"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/github_icon_pink"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="28dp"
android:textSize="30dp"
app:layout_constraintBottom_toBottomOf="@+id/iv_github"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.072"
app:layout_constraintStart_toEndOf="@+id/iv_github"
app:layout_constraintTop_toTopOf="@+id/iv_github"
app:layout_constraintVertical_bias="0.0"
tools:text="김다빈" /> //미리보기 역할
<TextView
android:id="@+id/tv_story"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40dp"
app:layout_constraintBottom_toBottomOf="@+id/iv_github"
app:layout_constraintEnd_toEndOf="@+id/tv_name"
app:layout_constraintStart_toEndOf="@+id/iv_github"
app:layout_constraintTop_toBottomOf="@+id/tv_name"
tools:text="인사말" />
</androidx.constraintlayout.widget.ConstraintLayout>
tools:text="인사말"
: 미리보기 역할하는 tools속성
~메인 액티비티~
package com.example.sopt_assignment_dabin
import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.RecyclerView
import com.example.sopt_assignment_dabin.databinding.ActivityRecyclerViewBinding
import com.example.sopt_assignment_dabin.databinding.ItemSampleListBinding
class RecyclerView_Activity : AppCompatActivity() {
private lateinit var binding: ActivityRecyclerViewBinding
private lateinit var myAdapter: RecyclerViewActivityAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityRecyclerViewBinding.inflate(layoutInflater)
setContentView(binding.root)
initAdapter() //Adapter만들고 연결하는 함수 모듈화 한 후 실행 시켜
}
private fun initAdapter() {
myAdapter = RecyclerViewActivityAdapter()
binding.container.adapter = myAdapter
myAdapter.list.addAll(
listOf(
Introduce("김다빈", "하이"),
Introduce("아아아", "하이"),
Introduce("사사사", "하이"),
Introduce("김다빈", "하이"),
Introduce("아아아", "하이"),
Introduce("사사사", "하이")
)
)
myAdapter.notifyDataSetChanged()
}
}
class RecyclerViewActivityAdapter(
) : RecyclerView.Adapter<RecyclerViewActivityAdapter.ViewHolder>() {
var list = mutableListOf<Introduce>()
class ViewHolder(private val view: ItemSampleListBinding) : RecyclerView.ViewHolder(view.root) {
fun onBind(data: Introduce) {
view.tvName.text = data.name
view.tvStory.text = data.story
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = ItemSampleListBinding.inflate(LayoutInflater.from(parent.context))
// val view=inflater.inflate(R.layout.item_sample_list,parent,false)
return ViewHolder(view)
}
override fun getItemCount() = list.size //한줄로 리턴되는 함수 가독성~
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
// holder.name.setText(list.get(position).name)
// holder.story.setText(list.get(position).story)
holder.onBind(list[position])
}
}
~메인 엑티비티의 레이아웃이자 RecyclerView를 띄우는 container~
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".RecyclerView_Activity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
tools:itemCount="4"
tools:listitem="@layout/item_sample_list"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Author And Source
이 문제에 관하여([안드로이드]RecyclerView_dataBinding), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@dabin/안드로이드RecyclerViewdataBinding저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)