Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
Tags
- 1차원 DP
- 2차원 dp
- 99클럽
- @GeneratedValue
- @GenericGenerator
- @Transactional
- Actions
- Amazon EFS
- amazon fsx
- Android Studio
- ANSI SQL
- async/await
- AVG
- AWS
- Azure
- bind
- builder
- button
- c++
- c++ builder
- c03
- Callback
- case when
- CCW
- chat GPT
- CICD
- Collections
- Combination
- combinations
- Comparator
Archives
- Today
- Total
기록
androidStudio/kotliln/recyclerview 사용하기 본문
개요
1. Gradle 라이브러리 추가
2. 리사이클러뷰에 연결할 객체 생성하기 [ Item Data Class 생성 ]
3. 리사이클러뷰 레이아웃 추가 [ recycler_layout.xml 추가 ]
4. 리사이클러뷰 어댑터 추가 [ MyAdapter.kt 추가 ]
5. 메인 프래그먼트에 리사이클러뷰 추가 [ MainFragment에 리사이클러뷰 초기화 코드, 레이아웃 추가 ]
1. build.gradle(:app)
// recyclerview
implementation "androidx.recyclerview:recyclerview:1.0.0"
2. add data model
data class Item(var title:String)
3. add recyclerview layout
3-1. recycler_layout.xml 작성
<?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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
4. add recyclerview Adapter
class MyAdapter(private val context: Context) :
RecyclerView.Adapter<MyAdapter.ViewHolder>() {
var dataset = mutableListOf<Item>()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyAdapter.ViewHolder {
val view = LayoutInflater.from(context)
.inflate(R.layout.recycler_layout, parent,false)
return ViewHolder(view)
}
override fun getItemCount(): Int = dataset.size
override fun onBindViewHolder(holder: MyAdapter.ViewHolder, position: Int) {
holder.bind(dataset[position])
}
inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view){
private val tv_title:TextView = view.findViewById(R.id.tv_title)
fun bind(item: Item){
tv_title.text = item.title
}
}
}
5. MainFragment에 recyclerview 추가
5-1. MainFragment.kt 에 recyclerview 초기화 코드 작성
class MainFragment : Fragment() {
lateinit var myAdapter: MyAdapter
lateinit var root:View
lateinit var recycler: RecyclerView
val dataset = mutableListOf<Item>()
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
root = inflater.inflate(R.layout.fragment_main, container, false)
recycler = root.findViewById(R.id.recycler)
myAdapter = MyAdapter(root.context)
initRecycler()
return root
}
private fun initRecycler(){
recycler.adapter = myAdapter
dataset.apply {
add(Item("1"))
add(Item("2"))
add(Item("3"))
myAdapter.dataset = dataset
myAdapter.notifyDataSetChanged()
}
}
}
5-2. fragment_main.xml에 recyclerview 추가
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
android:orientation="vertical"
tools:listitem="@layout/recycler_layout"/>
결과
참고자료
'Moblie > Android' 카테고리의 다른 글
[issue] Unrecognized Android Studio (or Android Support plugin for IntelliJ IDEA) version , please retry with version XXXX.X.X or newer. (0) | 2022.04.06 |
---|---|
androidStudio/상태바와 액션바 제거하기 (0) | 2022.04.04 |
androidStudio/java/공공데이터 api 사용하기 (0) | 2022.01.24 |
androidStudio/java/naver map api 사용하기 (0) | 2022.01.22 |
[issue] java.lang.SecurityException (0) | 2021.12.07 |
Comments