내가 쓰는 기술을 이해하고싶으면 그게 없었을때 어떻게 구현했는지를 보면 된다.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".TodoFragment">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_todo_fragment"
android:layout_width="match_parent"
tools:listitem="@layout/item_todo_recyclerview"
android:layout_height="match_parent" />
</FrameLayout>
<?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"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_margin="10dp"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tv_item_element"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="todo" />
</androidx.constraintlayout.widget.ConstraintLayout
class RecyclerviewAdapter :RecyclerView.Adapter<RecyclerviewAdapter.viewHolder>(){
class viewHolder(view: View) : RecyclerView.ViewHolder(view) {
val elementText: TextView = view.findViewById(R.id.tv_item_element)
}
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int
): RecyclerviewAdapter.viewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_todo_recyclerview, parent, false)
val holder = viewHolder(view)
return holder
}
override fun onBindViewHolder(holder: RecyclerviewAdapter.viewHolder, position: Int) {
holder.elementText.text = "todo"
}
override fun getItemCount(): Int {
return 100
}
}
class TodoFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.fragment_todo, container, false)
val recyclerview: RecyclerView = view.findViewById(R.id.rv_todo_fragment)
val adapter = RecyclerviewAdapter()
recyclerview.adapter = adapter
recyclerview.layoutManager = LinearLayoutManager(view.context, LinearLayoutManager.VERTICAL,false)
return view
}
}