Skip to content

Instantly share code, notes, and snippets.

@charlesmuchene
Created March 20, 2020 09:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save charlesmuchene/b030cff0299272c9643333cdb9f86172 to your computer and use it in GitHub Desktop.
Save charlesmuchene/b030cff0299272c9643333cdb9f86172 to your computer and use it in GitHub Desktop.
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