Answer the question
In order to leave comments, you need to log in
Why doesn't the LinearLayoutManager take a this parameter?
The NotesFragment should display a list using RecycleView. But for some reason, when creating a LinearLayoutManager(), the this or [email protected] parameter is underlined in red. What am I doing wrong?
class NotesFragment : Fragment() {
private var binding: FragmentListBinding? = null
private val adapter = NoteAdapter()
companion object {
private const val PARAMS_KEY = "listFragmentKey"
fun newInstance(params: ListFragmentParams) = NotesFragment().apply {
arguments = Bundle().apply { putParcelable(PARAMS_KEY, params) }
}
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = FragmentListBinding.inflate(layoutInflater)
return binding?.root
binding!!.rcList.layoutManager = LinearLayoutManager(this)
binding!!.rcList.adapter = adapter
}
override fun onDestroy() {
super.onDestroy()
binding = null
}
}
class NoteAdapter : RecyclerView.Adapter<NoteAdapter.NoteHolder>() {
private val noteList = ArrayList<String>()
class NoteHolder(item: View) : RecyclerView.ViewHolder(item) {
private val binding = MyListItemBinding.bind(item)
fun bind(authorText: String) = with(binding) {
tvMessage.text = authorText
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): NoteHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.my_list_item, parent, false)
return NoteHolder(view)
}
override fun onBindViewHolder(holder: NoteHolder, position: Int) {
holder.bind(noteList[position])
}
override fun getItemCount(): Int {
return noteList.size
}
fun addNote(note: String) {
noteList.add(note)
notifyDataSetChanged()
}
}
Answer the question
In order to leave comments, you need to log in
But for some reason, when creating a LinearLayoutManager(), the this or [email protected] parameter is underlined in red
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question