

class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
private val dataList: ArrayList<DataItem> = ArrayList()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
initializeData()
binding.recyclerview.adapter = MyAdapter(dataList)
binding.recyclerview.layoutManager = LinearLayoutManager(this@MainActivity)
}
fun initializeData() {
dataList.add(DataItem("num1", "eunji", Code.RIGHT_CONTENT))
dataList.add(DataItem("num2", "eunji2", Code.CENTER_CONTENT))
dataList.add(DataItem("num3", "eunji3", Code.LEFT_CONTENT))
dataList.add(DataItem("num4", "eunji4", Code.RIGHT_CONTENT))
}
}
class DataItem {
var content : String
var name : String
var viewType : Int
constructor(name: String, content: String, viewType: Int){
this.content=content
this.name=name
this.viewType=viewType
}
}
class Code {
companion object {
public val LEFT_CONTENT = 0
public val RIGHT_CONTENT = 1
public val CENTER_CONTENT = 2
}
}
//일반적으로 하나의 뷰홀더(하나의 레이아웃)만 있는 리사이클러 뷰와는 다르게
//뷰타입에 따라 레이아웃을 각각 만들고 Adpater클래스에서 뷰타입에 따라 각각 뷰홀더 및 필요한 함수를 만들어준다.
class MyAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder> {
private var myDataList: ArrayList<DataItem>
constructor(dataList: ArrayList<DataItem>) {
myDataList = dataList
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
val view: View
val context = parent.context
val inflater = context.getSystemService(LAYOUT_INFLATER_SERVICE) as LayoutInflater
if (viewType == Code.CENTER_CONTENT) {
view = inflater.inflate(R.layout.center_content, parent, false)
return CenterViewHolder(view)
} else if (viewType == Code.LEFT_CONTENT) {
view = inflater.inflate(R.layout.left_content, parent, false)
return LeftViewHolder(view)
} else {
view = inflater.inflate(R.layout.right_content, parent, false)
return RightViewHolder(view)
}
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
if (holder is CenterViewHolder) {
holder.content.setText(myDataList[position].content)
} else if (holder is LeftViewHolder) {
holder.name.setText(myDataList[position].name);
holder.content.setText(myDataList[position].content);
} else if (holder is RightViewHolder) {
holder.name.setText(myDataList[position].name);
holder.content.setText(myDataList[position].content);
}
}
override fun getItemCount(): Int {
return myDataList.size
}
override fun getItemViewType(position: Int): Int {
return myDataList[position].viewType
}
inner class CenterViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val content: TextView
init {
content = itemView.findViewById(R.id.content)
}
}
inner class LeftViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val content: TextView
val name: TextView
init {
content = itemView.findViewById(R.id.content)
name = itemView.findViewById(R.id.name)
}
}
inner class RightViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val content: TextView
val name: TextView
init {
content = itemView.findViewById(R.id.content)
name = itemView.findViewById(R.id.name)
}
}
}
<?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=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="<http://schemas.android.com/apk/res/android>"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="<http://schemas.android.com/apk/res-auto>">
<TextView
android:id="@+id/content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="TextView"
android:textSize="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="<http://schemas.android.com/apk/res/android>"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="<http://schemas.android.com/apk/res-auto>">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@mipmap/ic_launcher_round" />
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"
android:text="TextView"
app:layout_constraintLeft_toRightOf="@id/imageView"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:text="TextView"
app:layout_constraintLeft_toRightOf="@id/imageView"
app:layout_constraintTop_toBottomOf="@id/name" />
</androidx.constraintlayout.widget.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="<http://schemas.android.com/apk/res/android>"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="<http://schemas.android.com/apk/res-auto>">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_marginTop="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@mipmap/ic_launcher_round" />
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_marginTop="20dp"
android:text="TextView"
app:layout_constraintRight_toLeftOf="@id/imageView"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:text="TextView"
app:layout_constraintRight_toLeftOf="@id/imageView"
app:layout_constraintTop_toBottomOf="@id/name" />
</androidx.constraintlayout.widget.ConstraintLayout>