Android Application
Kotlin 고급 위젯 - 뷰 컨테이너(View Container) : 리사이클러뷰(RecyclerView)
작성자 임베디드코리아
작성일23-09-24 02:59
조회1,585회
댓글0건
------------------------------------------------------------------------------------
activity_main.xml
-------------------------------------------------------------------------------------
<?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/rv_board"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:listitem="@layout/item_recycler_view" />
</androidx.constraintlayout.widget.ConstraintLayout>
------------------------------------------------------------------------------------
MainActivity.kt
-------------------------------------------------------------------------------------
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val rv_board = findViewById<RecyclerView>(R.id.rv_board)
val itemList = ArrayList<BoardItem>()
itemList.add(BoardItem("13:00","월급 두배로 받는법","김XX"))
itemList.add(BoardItem("11:00","학점 A+ 받는 법","이XX"))
itemList.add(BoardItem("10:00","구글 면접 질문에 대답하는 법","박XX"))
itemList.add(BoardItem("08:00","공부 잘하는 MBTI 순위","최XX"))
val boardAdapter = BoardAdapter(itemList)
boardAdapter.notifyDataSetChanged()
rv_board.adapter = boardAdapter
rv_board.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
}
}
------------------------------------------------------------------------------------
BoardItem.kt
-------------------------------------------------------------------------------------
class BoardItem (val time: String, val title: String, val name: String)
------------------------------------------------------------------------------------
BoardAdapter.kt
-------------------------------------------------------------------------------------
class BoardAdapter(val itemList: ArrayList<BoardItem>) :
RecyclerView.Adapter<BoardAdapter.BoardViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BoardViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_recycler_view, parent, false)
return BoardViewHolder(view)
}
override fun onBindViewHolder(holder: BoardViewHolder, position: Int) {
holder.tv_time.text = itemList[position].time
holder.tv_title.text = itemList[position].title
holder.tv_name.text = itemList[position].name
}
override fun getItemCount(): Int {
return itemList.count()
}
inner class BoardViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val tv_time = itemView.findViewById<TextView>(R.id.tv_time)
val tv_title = itemView.findViewById<TextView>(R.id.tv_title)
val tv_name = itemView.findViewById<TextView>(R.id.tv_name)
}
}
------------------------------------------------------------------------------------
BoardViewHolder.kt
-------------------------------------------------------------------------------------
class BoardViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val tv_time = itemView.findViewById<TextView>(R.id.tv_time)
val tv_title = itemView.findViewById<TextView>(R.id.tv_title)
val tv_name = itemView.findViewById<TextView>(R.id.tv_name)
}
------------------------------------------------------------------------------------
*** item_recycler_view.xml --> res/layout
-------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingHorizontal="15dp"
android:paddingVertical="20dp">
<TextView
android:id="@+id/tv_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="13:42" />
<TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingHorizontal="10dp"
android:text="글 제목" />
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="홍길동" />
</LinearLayout>