Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Fragment hosting a recycler view
class HomeFragment : Fragment() {
private val adapter = Adapter()
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? =
inflater.inflate(R.layout.fragment_home, container, false)
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val recyclerView: RecyclerView = view.findViewById(R.id.recycler_view)
recyclerView.layoutManager = LinearLayoutManager(this.context)
recyclerView.adapter = adapter
}
}
class ViewHolder(view: View) : RecyclerView.ViewHolder(view)
class Adapter : RecyclerView.Adapter<ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder =
ViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.view_item, parent, false))
override fun getItemCount(): Int = 10
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
// Bind your data at this position to the view holder
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.